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부터) |
|
명시적으로 기본 정의된 생성자들로 인해 반환 타입은 할당 가능(assignable)하지 않습니다. |
(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를 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 stateless return type
반환 타입은 CopyConstructible stateless 클래스입니다. 반환 타입이 assignable인지 여부는 명시되지 않습니다.
멤버 함수 operator ( )
|
template
<
class
...
Args
>
constexpr
auto
operator
(
)
(
Args
&&
...
args
)
const
|
(since C++26) | |
Expression-equivalent 는 ! 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) |
호출 가능 객체를 상수 템플릿 인수로
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
객체를 생성합니다
(함수 템플릿) |