Skip to content

Files

Latest commit

7ae4be4 · Apr 3, 2025

History

History
144 lines (106 loc) · 5.15 KB

not_fn.md

File metadata and controls

144 lines (106 loc) · 5.15 KB

not_fn

  • functional[meta header]
  • std[meta namespace]
  • function template[meta id-type]
  • cpp17[meta cpp]
namespace std {
  template <class F>
  unspecified not_fn(F&& f);            // (1) C++17
  template <class F>
  constexpr unspecified not_fn(F&& f);  // (1) C++20

  template <auto f>
  constexpr unspecified not_fn();  // (2) C++26
}
  • unspecified[italic]

概要

任意個数の引数をとってbool型を返す関数オブジェクトを受け取り、戻り値を論理反転する関数オブジェクトに変換する。

事前条件

  • (1) : decay_t<F>を適用した型をFDとして、FDがCpp17MoveConstructible要件を満たすこと

適格要件

効果(C++17)

説明用の関数オブジェクトcall_wrapperがあるものとして、call_wrapper(std::forward<F>(f))を返す。

説明用の関数オブジェクトcall_wrapperは、以下のようなクラスである:

class call_wrapper {
  using FD = decay_t<F>;

  explicit call_wrapper(F&& f); // not_fnをfriendにして呼び出す

public:
  call_wrapper(call_wrapper&&) = default;
  call_wrapper(call_wrapper const&) = default;

  template <class... Args>
  auto operator()(Args&&...) & -> decltype(!declval<invoke_result_t<FD&, Args&&...>>());

  template <class... Args>
  auto operator()(Args&&...) const& -> decltype(!declval<invoke_result_t<FD const&, Args&&...>>());

  template <class... Args>
  auto operator()(Args&&...) && -> decltype(!declval<invoke_result_t<FD, Args&&...>>());

  template <class... Args>
  auto operator()(Args&&...) const&& -> decltype(!declval<invoke_result_t<FD const, Args&&...>>());

private:
  FD fd;
};
  • decay_t[link /reference/type_traits/decay.md]
  • declval[link /reference/utility/declval.md]
  • invoke_result_t[link /reference/type_traits/invoke_result.md]

このクラスのコンストラクタは、式fd = std::forward<F>(f)を実行する。この式が例外を送出する可能性がある。

このクラスの関数オブジェクトは、以下の式を実行する:

戻り値(C++20)

結果オブジェクトgに対する関数呼び出し式の引数パックcall_argsとしたとき

  • (1) : fdstd::forward<F>(f))で直接非リスト初期化したFD型のオブジェクトに対して
    • 関数呼び出し式の結果が!invoke(fd, call_args...)に等しい、完全転送呼び出しラッパー(perfect forwarding call wrapper)オブジェクトを返す。
  • (2) :
    • 関数呼び出し式の結果が!invoke(f, call_args...)に等しい、状態を持たない完全転送呼び出しラッパー(perfect forwarding call wrapper)オブジェクトを返す。

例外

  • (1) : 関数オブジェクトfのムーブによって任意の例外が送出される可能性がある

#include <iostream>
#include <functional>

bool pred_func(int, char, double)
{
  return true;
}

struct pred_functor {
  bool operator()(double, int)
  {
    return false;
  }
};

int main()
{
  std::cout << std::boolalpha;

  auto not_func = std::not_fn(pred_func);
  std::cout << not_func(1, 'a', 3.14) << std::endl;

  auto not_functor = std::not_fn(pred_functor{});
  std::cout << not_functor(3.14, 1) << std::endl;
}
  • std::not_fn[color ff0000]

出力

false
true

バージョン

言語

  • C++17

処理系

参照