std:: not_fn
|
Definiert im Header
<functional>
|
||
|
template
<
class
F
>
/* unspecified */ not_fn ( F && f ) ; |
(1) |
(seit C++17)
(constexpr seit C++20) |
|
template
<
auto
ConstFn
>
constexpr /* unspecified */ not_fn ( ) noexcept ; |
(2) | (seit C++26) |
ConstFn
ein Nullzeiger oder Null-Memberzeiger ist.
Inhaltsverzeichnis |
Parameter
| f | - | das Objekt, von dem das Callable -Objekt, das vom Wrapper gehalten wird, konstruiert wird |
| Typanforderungen | ||
-
std::
decay_t
<
F
>
muss die Anforderungen von
Callable
und
MoveConstructible
erfüllen.
|
||
|
-
|
||
Rückgabewert
T
. It has the following members.
std::not_fn Rückgabetyp
Member-Objekte
Der Rückgabetyp von
std::not_fn
enthält ein Mitgliedsobjekt vom Typ
std::
decay_t
<
F
>
.
Konstruktoren
|
explicit
T
(
F
&&
f
)
;
|
(1) |
(seit C++17)
(constexpr seit C++20) ( Nur zur Darstellung* ) |
|
T
(
T
&&
f
)
=
default
;
T ( const T & f ) = default ; |
(2) | (seit C++17) |
|
Die explizit standarddefinierten Definitionen bewirken, dass der Rückgabetyp nicht zuweisbar ist. |
(bis C++20) |
|
Es ist nicht spezifiziert, ob diese Konstruktoren explizit standarddefiniert sind und ob der Rückgabetyp zuweisbar ist. |
(seit C++20) |
Member-Funktion operator ( )
| (1) | ||
|
template
<
class
...
Args
>
auto
operator
(
)
(
Args
&&
...
args
)
&
|
(seit C++17)
(bis C++20) |
|
|
template
<
class
...
Args
>
constexpr
auto
operator
(
)
(
Args
&&
...
args
)
&
|
(seit C++20) | |
| (2) | ||
|
template
<
class
...
Args
>
auto
operator
(
)
(
Args
&&
...
args
)
&&
|
(seit C++17)
(bis C++20) |
|
|
template
<
class
...
Args
>
constexpr
auto
operator
(
)
(
Args
&&
...
args
)
&&
|
(seit C++20) | |
Sei fd das Member-Objekt vom Typ std:: decay_t < F > .
|
1)
Entspricht
return
!
std::
invoke
(
fd,
std::
forward
<
Args
>
(
args
)
...
)
;
2)
Entspricht
return
!
std::
invoke
(
std
::
move
(
fd
)
,
std::
forward
<
Args
>
(
args
)
...
)
;
Während des Aufrufs des Ergebnisses, falls die Substitution in den Rückgabetyp der ursprünglich ausgewählten operator ( ) -Überladung fehlschlägt, kann eine andere Überladung ausgewählt werden . |
(seit C++17)
(bis C++20) |
|
2)
Ausdrucksäquivalent
zu
!
std::
invoke
(
std
::
move
(
fd
)
,
std::
forward
<
Args
>
(
args
)
...
)
Während des Aufrufs des Ergebnisses, falls die Substitution in den Rückgabetyp der ursprünglich ausgewählten operator ( ) -Überladung fehlschlägt, ist der Aufruf fehlerhaft, was auch ein Substitutionsfehler sein kann. |
(seit C++20) |
std::not_fn stateless return type
Der Rückgabetyp ist eine CopyConstructible stateless Klasse. Es ist nicht spezifiziert, ob der Rückgabetyp zuweisbar ist.
Member-Funktion operator ( )
|
template
<
class
...
Args
>
constexpr
auto
operator
(
)
(
Args
&&
...
args
)
const
|
(since C++26) | |
Ausdrucksäquivalent zu ! std:: invoke ( ConstFn, std:: forward < Args > ( args ) ... ) .
Exceptions
Mögliche Implementierung
| (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)...); } // Gelöschte Überladungen sind seit C++20 erforderlich // um zu verhindern, dass eine nicht-äquivalente, aber wohlgeformte Überladung ausgewählt wird. 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 {}; } |
Hinweise
std::not_fn
soll die Negatoren aus der C++03-Ära
std::not1
und
std::not2
ersetzen.
| Feature-Test Makro | Wert | Std | Feature |
|---|---|---|---|
__cpp_lib_not_fn
|
201603L
|
(C++17) |
std::not_fn()
,
(
1
)
|
202306L
|
(C++26) |
Erlaubt das Übergeben von aufrufbaren Objekten als konstante Template-Argumente an
std::not_fn
,
(
2
)
|
Beispiel
#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() { // Verwendung mit einer freien Funktion: auto is_differ = std::not_fn(is_same); assert(is_differ(8, 8) == false); // äquivalent zu: !is_same(8, 8) == false assert(is_differ(6, 9) == true); // äquivalent zu: !is_same(8, 0) == true // Verwendung mit einer Member-Funktion: 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-Spezifikation bleibt erhalten: static_assert(noexcept(is_differ) == noexcept(is_same)); static_assert(noexcept(member_differ) == noexcept(&S::is_same)); // Verwendung mit einem Funktionsobjekt: 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 }
Siehe auch
|
(deprecated in C++17)
(removed in C++20)
|
Konstruiert benutzerdefiniertes
std::unary_negate
Objekt
(Funktions-Template) |
|
(deprecated in C++17)
(removed in C++20)
|
Konstruiert benutzerdefiniertes
std::binary_negate
Objekt
(Funktions-Template) |