- functional[meta header]
- std[meta namespace]
- function template[meta id-type]
- cpp17[meta cpp]
namespace std {
template <class F>
unspecified not_fn(F&& f); //C++17
template <class F>
constexpr unspecified not_fn(F&& f); //C++20
}- unspecified[italic]
任意個数の引数をとってbool型を返す関数オブジェクトを受け取り、戻り値を論理反転する関数オブジェクトに変換する。
decay_t<F>を適用した型をFDとして、
FDがstd::move_constructible要件を満たすこと
decay_t<F>を適用した型をFDとして、
is_constructible_v<FD, F>がtrueであることis_move_constructible_v<FD>がtrueであること
説明用の関数オブジェクト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)を実行する。この式が例外を送出する可能性がある。
このクラスの関数オブジェクトは、以下の式を実行する:
- 左辺値参照版 :
return !INVOKE(fd,std::forward<Args>(args)...) - 右辺値参照版 :
return !INVOKE(std::move(fd),std::forward<Args>(args)...)
- 関数オブジェクト
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
- Clang: 3.9.1
- GCC: 7.2
- ICC: ??
- Visual C++: ??