std:: not_fn
|
ヘッダーで定義
<functional>
|
||
|
template
<
class
F
>
/* unspecified */ not_fn ( F && f ) ; |
(1) |
(C++17以降)
(C++20以降constexpr) |
|
template
<
auto
ConstFn
>
constexpr /* unspecified */ not_fn ( ) noexcept ; |
(2) | (C++26以降) |
ConstFn
がヌルポインタまたはヌルメンバポインタの場合、プログラムは不適格となります。
目次 |
パラメータ
| f | - | ラッパーが保持する Callable オブジェクトの構築元となるオブジェクト |
| 型要件 | ||
-
std::
decay_t
<
F
>
は
Callable
および
MoveConstructible
の要件を満たさなければならない。
|
||
|
-
|
||
戻り値
T
. It has the following members.
std::not_fn 戻り値の型
メンバーオブジェクト
std::not_fn
の戻り値型は、
std::
decay_t
<
F
>
型のメンバオブジェクトを保持します。
コンストラクタ
|
explicit
T
(
F
&&
f
)
;
|
(1) |
(C++17以降)
(C++20以降 constexpr) ( 説明専用* ) |
|
T
(
T
&&
f
)
=
default
;
T ( const T & f ) = default ; |
(2) | (C++17以降) |
|
明示的にデフォルト定義されたため、戻り値の型は代入可能ではない。 |
(C++20まで) |
|
これらのコンストラクタが明示的にデフォルト化されているかどうか、および戻り値の型が代入可能かどうかは未規定である。 |
(C++20以降) |
メンバー関数 operator ( )
| (1) | ||
|
template
<
class
...
Args
>
auto
operator
(
)
(
Args
&&
...
args
)
&
|
(C++17以降)
(C++20まで) |
|
|
template
<
class
...
Args
>
constexpr
auto
operator
(
)
(
Args
&&
...
args
)
&
|
(C++20以降) | |
| (2) | ||
|
template
<
class
...
Args
>
auto
operator
(
)
(
Args
&&
...
args
)
&&
|
(C++17以降)
(C++20まで) |
|
|
template
<
class
...
Args
>
constexpr
auto
operator
(
)
(
Args
&&
...
args
)
&&
|
(C++20以降) | |
fd を型 std:: decay_t < F > のメンバーオブジェクトとする。
|
1)
以下と等価:
return
!
std::
invoke
(
fd,
std::
forward
<
Args
>
(
args
)
...
)
;
2)
以下と等価:
return
!
std::
invoke
(
std
::
move
(
fd
)
,
std::
forward
<
Args
>
(
args
)
...
)
;
結果を呼び出す際に、元々選択された operator ( ) オーバーロードの戻り値型への置換が失敗した場合、 別のオーバーロードが選択される可能性がある 。 |
(C++17以降)
(C++20まで) |
|
結果を呼び出す際に、元々選択された operator ( ) オーバーロードの戻り値型への置換が失敗した場合、呼び出しは不適格となり、これは 置換失敗 となる可能性もある。 |
(C++20以降) |
std::not_fn ステートレス戻り値型
戻り値型は CopyConstructible なステートレスなクラスである。戻り値型が代入可能かどうかは未規定。
メンバ関数 operator ( )
|
template
<
class
...
Args
>
constexpr
auto
operator
(
)
(
Args
&&
...
args
)
const
|
(C++26以降) | |
式等価 は ! std:: invoke ( ConstFn, std:: forward < Args > ( args ) ... ) である。
例外
実装例
| (1) not_fn |
|---|
namespace detail { template<class V, class F, class... Args> constexpr bool negate_invocable_impl = false; template<class F, class... Args> constexpr bool negate_invocable_impl<std::void_t<decltype( !std::invoke(std::declval<F>(), std::declval<Args>()...))>, F, Args...> = true; template<class F, class... Args> constexpr bool negate_invocable_v = negate_invocable_impl<void, F, Args...>; template<class F> struct not_fn_t { F f; template<class... Args, std::enable_if_t<negate_invocable_v<F&, Args...>, int> = 0> constexpr decltype(auto) operator()(Args&&... args) & noexcept(noexcept(!std::invoke(f, std::forward<Args>(args)...))) { return !std::invoke(f, std::forward<Args>(args)...); } template<class... Args, std::enable_if_t<negate_invocable_v<const F&, Args...>, int> = 0> constexpr decltype(auto) operator()(Args&&... args) const& noexcept(noexcept(!std::invoke(f, std::forward<Args>(args)...))) { return !std::invoke(f, std::forward<Args>(args)...); } template<class... Args, std::enable_if_t<negate_invocable_v<F, Args...>, int> = 0> constexpr decltype(auto) operator()(Args&&... args) && noexcept(noexcept(!std::invoke(std::move(f), std::forward<Args>(args)...))) { return !std::invoke(std::move(f), std::forward<Args>(args)...); } template<class... Args, std::enable_if_t<negate_invocable_v<const F, Args...>, int> = 0> constexpr decltype(auto) operator()(Args&&... args) const&& noexcept(noexcept(!std::invoke(std::move(f), std::forward<Args>(args)...))) { return !std::invoke(std::move(f), std::forward<Args>(args)...); } // 削除されたオーバーロードは C++20 以降で必要 // 非等価だが適切に形成されたオーバーロードが選択されるのを防ぐため。 template<class... Args, std::enable_if_t<!negate_invocable_v<F&, Args...>, int> = 0> void operator()(Args&&...) & = delete; template<class... Args, std::enable_if_t<!negate_invocable_v<const F&, Args...>, int> = 0> void operator()(Args&&...) const& = delete; template<class... Args, std::enable_if_t<!negate_invocable_v<F, Args...>, int> = 0> void operator()(Args&&...) && = delete; template<class... Args, std::enable_if_t<!negate_invocable_v<const F, Args...>, int> = 0> void operator()(Args&&...) const&& = delete; }; } template<class F> constexpr detail::not_fn_t<std::decay_t<F>> not_fn(F&& f) { return {std::forward<F>(f)}; } |
| (2) not_fn |
namespace detail { template<auto ConstFn> struct stateless_not_fn { template<class... Args> constexpr auto operator()(Args&&... args) const noexcept(noexcept(!std::invoke(ConstFn, std::forward<Args>(args)...))) -> decltype(!std::invoke(ConstFn, std::forward<Args>(args)...)) { return !std::invoke(ConstFn, std::forward<Args>(args)...); } }; } template<auto ConstFn> constexpr detail::stateless_not_fn<ConstFn> not_fn() noexcept { if constexpr (std::is_pointer_v<decltype(ConstFn)> || std::is_member_pointer_v<decltype(ConstFn)>) static_assert(ConstFn != nullptr); return {}; } |
注記
std::not_fn
は、C++03時代の否定関数オブジェクト
std::not1
および
std::not2
を置き換えることを意図しています。
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_not_fn
|
201603L
|
(C++17) |
std::not_fn()
,
(
1
)
|
202306L
|
(C++26) |
callableオブジェクトを定数テンプレート引数として
std::not_fn
に渡すことを許可,
(
2
)
|
例
#include <cassert> #include <functional> bool is_same(int a, int b) noexcept { return a == b; } struct S { int val; bool is_same(int arg) const noexcept { return val == arg; } }; int main() { // フリー関数の使用: auto is_differ = std::not_fn(is_same); assert(is_differ(8, 8) == false); // 等価: !is_same(8, 8) == false assert(is_differ(6, 9) == true); // 等価: !is_same(8, 0) == true // メンバー関数の使用: auto member_differ = std::not_fn(&S::is_same); assert(member_differ(S{3}, 3) == false); //: S tmp{6}; !tmp.is_same(6) == false // noexcept指定は保持される: static_assert(noexcept(is_differ) == noexcept(is_same)); static_assert(noexcept(member_differ) == noexcept(&S::is_same)); // 関数オブジェクトの使用: auto same = [](int a, int b) { return a == b; }; auto differ = std::not_fn(same); assert(differ(1, 2) == true); //: !same(1, 2) == true assert(differ(2, 2) == false); //: !same(2, 2) == false #if __cpp_lib_not_fn >= 202306L auto is_differ_cpp26 = std::not_fn<is_same>(); assert(is_differ_cpp26(8, 8) == false); assert(is_differ_cpp26(6, 9) == true); auto member_differ_cpp26 = std::not_fn<&S::is_same>(); assert(member_differ_cpp26(S{3}, 3) == false); auto differ_cpp26 = std::not_fn<same>(); static_assert(differ_cpp26(1, 2) == true); static_assert(differ_cpp26(2, 2) == false); #endif }
関連項目
|
(deprecated in C++17)
(removed in C++20)
|
カスタム
std::unary_negate
オブジェクトを構築する
(関数テンプレート) |
|
(deprecated in C++17)
(removed in C++20)
|
カスタム
std::binary_negate
オブジェクトを構築する
(関数テンプレート) |