Skip to content

Commit cec4487

Browse files
author
Colin Robertson
committed
Add not_fn per TC workitem 1203081
1 parent 0d1a8aa commit cec4487

File tree

2 files changed

+126
-14
lines changed

2 files changed

+126
-14
lines changed

docs/standard-library/functional-functions.md

Lines changed: 117 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "<functional> functions"
3-
ms.date: "11/04/2016"
4-
f1_keywords: ["functional/std::bind", "xfunctional/std::bind1st", "xfunctional/std::bind2nd", "xfunctional/std::bit_and", "xfunctional/std::bit_not", "xfunctional/std::bit_or", "xfunctional/std::bit_xor", "functional/std::cref", "type_traits/std::cref", "xfunctional/std::mem_fn", "xfunctional/std::mem_fun_ref", "xfunctional/std::not1", "xfunctional/std::not2", "xfunctional/std::ptr_fun", "functional/std::ref", "functional/std::swap"]
3+
ms.date: "01/17/2019"
4+
f1_keywords: ["functional/std::bind", "xfunctional/std::bind1st", "xfunctional/std::bind2nd", "xfunctional/std::bit_and", "xfunctional/std::bit_not", "xfunctional/std::bit_or", "xfunctional/std::bit_xor", "functional/std::cref", "type_traits/std::cref", "xfunctional/std::mem_fn", "xfunctional/std::mem_fun_ref", "xfunctional/std::not1", "xfunctional/std::not2", "functional/std::not_fn", "xfunctional/std::ptr_fun", "functional/std::ref", "functional/std::swap"]
55
helpviewer_keywords: ["std::bind [C++]", "std::bind1st", "std::bind2nd", "std::bit_and [C++]", "std::bit_not [C++]", "std::bit_or [C++]", "std::bit_xor [C++]", "std::cref [C++]"]
66
ms.assetid: c34d0b45-50a7-447a-9368-2210d06339a4
77
---
@@ -13,8 +13,8 @@ ms.assetid: c34d0b45-50a7-447a-9368-2210d06339a4
1313
|[bit_and](#bit_and)|[bit_not](#bit_not)|[bit_or](#bit_or)|
1414
|[bit_xor](#bit_xor)|[cref](#cref)|[mem_fn](#mem_fn)|
1515
|[mem_fun](#mem_fun)|[mem_fun_ref](#mem_fun_ref)|[not1](#not1)|
16-
|[not2](#not2)|[ptr_fun](#ptr_fun)|[ref](#ref)|
17-
|[swap](#swap)|
16+
|[not2](#not2)|[not_fn](#not_fn)|[ptr_fun](#ptr_fun)|
17+
|[ref](#ref)|[swap](#swap)|
1818

1919
## <a name="bind"></a> bind
2020

@@ -777,7 +777,7 @@ With the even numbers removed, the remaining values are: 1 3 5 7 9 11 13
777777

778778
## <a name="not1"></a> not1
779779

780-
Returns the complement of a unary predicate.
780+
Returns the complement of a unary predicate. Deprecated in favor of [not_fn](#not_fn) in C++17.
781781

782782
```cpp
783783
template <class UnaryPredicate>
@@ -849,7 +849,7 @@ The number of elements in v1 not greater than 10 is: 3.
849849

850850
## <a name="not2"></a> not2
851851

852-
Returns the complement of a binary predicate.
852+
Returns the complement of a binary predicate. Deprecated in favor of [not_fn](#not_fn) in C++17.
853853

854854
```cpp
855855
template <class BinaryPredicate>
@@ -923,6 +923,117 @@ Sorted vector v1 = ( 41 6262 6262 6334 18467 19169 26500 )
923923
Resorted vector v1 = ( 26500 19169 18467 6334 6262 6262 41 )
924924
```
925925

926+
## <a name="not_fn"></a> not_fn
927+
928+
Intended as a generic replacement for negation function wrappers `std::not1` and `std::not2`, this function template creates a forwarding call wrapper that returns the logical negation of the result of its contained callable object. It preserves the const qualification and value category behavior of the wrapped function object. This template function is new in C++17, and replaces the deprecated `std::not1`, `std::not2`, `std::unary_negate` and `std::binary_negate`.
929+
930+
```cpp
931+
template <class Callable>
932+
unspecified not_fn(Callable&& func);
933+
```
934+
935+
### Parameters
936+
937+
*func*<br/>
938+
A callable object used to construct the forwarding call wrapper.
939+
940+
### Remarks
941+
942+
The template function returns a call wrapper equivalent to `return call_wrapper(std::forward<Callable>(func))` based on this exposition-only class:
943+
944+
```cpp
945+
class call_wrapper
946+
{
947+
using FD = decay_t<Callable>;
948+
explicit call_wrapper(Callable&& func);
949+
950+
public:
951+
call_wrapper(call_wrapper&&) = default;
952+
call_wrapper(call_wrapper const&) = default;
953+
954+
template<class... Args>
955+
auto operator()(Args&&...) & -> decltype(!declval<result_of_t<FD&(Args...)>>());
956+
957+
template<class... Args>
958+
auto operator()(Args&&...) const& -> decltype(!declval<result_of_t<FD const&(Args...)>>());
959+
960+
template<class... Args>
961+
auto operator()(Args&&...) && -> decltype(!declval<result_of_t<FD(Args...)>>());
962+
963+
template<class... Args>
964+
auto operator()(Args&&...) const&& -> decltype(!declval<result_of_t<FD const(Args...)>>());
965+
966+
private:
967+
FD fd;
968+
};
969+
```
970+
971+
The explicit constructor on the callable object *func* requires type `std::decay_t<Callable>` to satisfy the requirements of `MoveConstructible`, and `is_constructible_v<FD, Callable>` must be true. It initializes the wrapped callable object `fd` from `std::forward<Callable>(func)`, and throws any exception thrown by construction of `fd`.
972+
973+
The wrapper exposes call operators distinguished by lvalue or rvalue reference category and const qualification as shown here,
974+
975+
```cpp
976+
template<class... Args> auto operator()(Args&&... args) & -> decltype(!declval<result_of_t<FD&(Args...)>>());
977+
template<class... Args> auto operator()(Args&&... args) const& -> decltype(!declval<result_of_t<FD const&(Args...)>>());
978+
template<class... Args> auto operator()(Args&&... args) && -> decltype(!declval<result_of_t<FD(Args...)>>());
979+
template<class... Args> auto operator()(Args&&... args) const&& -> decltype(!declval<result_of_t<FD const(Args...)>>());
980+
```
981+
982+
The first two are equivalent to `return !INVOKE(fd, std::forward<Args>(args)...)`, and the second two are equivalent to `return !INVOKE(std::move(fd), std::forward<Args>(args)...)`.
983+
984+
### Example
985+
986+
```cpp
987+
// functional_not_fn_.cpp
988+
// compile with: /EHsc /std:c++17
989+
#include <vector>
990+
#include <algorithm>
991+
#include <functional>
992+
#include <cstdlib>
993+
#include <iostream>
994+
995+
int main()
996+
{
997+
using namespace std;
998+
vector <int> v1;
999+
1000+
int i;
1001+
v1.push_back( 6262 );
1002+
v1.push_back( 6262 );
1003+
for ( i = 0 ; i < 5 ; i++ )
1004+
{
1005+
v1.push_back(rand());
1006+
}
1007+
1008+
cout << "Original vector v1 = ( " ;
1009+
for (const auto& item : v1)
1010+
cout << item << " ";
1011+
cout << ")" << endl;
1012+
1013+
// To sort in ascending order,
1014+
// use default binary predicate less<>()
1015+
sort(v1.begin(), v1.end());
1016+
cout << "Sorted vector v1 = ( " ;
1017+
for (const auto& item : v1)
1018+
cout << item << " ";
1019+
cout << ")" << endl;
1020+
1021+
// To sort in descending order,
1022+
// use the helper function not_fn
1023+
sort(v1.begin(), v1.end(), not_fn(less<>()));
1024+
cout << "Resorted vector v1 = ( " ;
1025+
for (const auto& item : v1)
1026+
cout << item << " ";
1027+
cout << ")" << endl;
1028+
}
1029+
```
1030+
1031+
```Output
1032+
Original vector v1 = ( 6262 6262 41 18467 6334 26500 19169 )
1033+
Sorted vector v1 = ( 41 6262 6262 6334 18467 19169 26500 )
1034+
Resorted vector v1 = ( 26500 19169 18467 6334 6262 6262 41 )
1035+
```
1036+
9261037
## <a name="ptr_fun"></a> ptr_fun
9271038

9281039
Helper template functions used to convert unary and binary function pointers, respectively, into unary and binary adaptable functions.

docs/standard-library/functional.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
22
title: "&lt;functional&gt;"
3-
ms.date: "11/04/2016"
3+
ms.date: "01/17/2019"
44
f1_keywords: ["<functional>", "functional/std::<functional>", "std::<functional>"]
55
helpviewer_keywords: ["functors", "functional header"]
66
ms.assetid: 7dd463e8-a29f-49bc-aedd-8fa53b54bfbc
77
---
88
# &lt;functional&gt;
99

10-
Defines C++ Standard Library functions that help construct *function objects*also known as functorsand their binders. A function object is an object of a type that defines `operator()`. A function object can be a function pointer, but more typically, the object is used to store additional information that can be accessed during a function call.
10+
Defines C++ Standard Library functions that help construct *function objects*, also known as *functors*, and their binders. A function object is an object of a type that defines `operator()`. A function object can be a function pointer, but more typically, the object is used to store additional information that can be accessed during a function call.
1111

1212
## Syntax
1313

@@ -17,9 +17,9 @@ Defines C++ Standard Library functions that help construct *function objects*—
1717

1818
## Remarks
1919

20-
Algorithms require two types of function objects: unary and binary. Unary function objects require one argument, and binary function objects require two arguments. A function object and function pointers can be passed as a predicate to an algorithm, but function objects are also adaptable and increase the scope, flexibility, and efficiency of the C++ Standard Library. If, for example, a value needed to be bound to a function before being passed to an algorithm, then a function pointer could not be used. Function adaptors convert function pointers into adaptable function objects that can be bound to a value. The header \<functional> also contains member function adaptors that allow member functions to be called as adaptable function objects. Functions are adaptable if they have nested type declarations specifying their argument and return types. The C++ Standard requires that this adaptability is implemented by having all standard object classes inherit from the unary_function or binary_function base classes. Function objects and their adaptors allow the C++ Standard Library to upgrade existing applications and help integrate the library into the C++ programming environment.
20+
Algorithms require two types of function objects: *unary* and *binary*. Unary function objects require one argument, and binary function objects require two arguments. A function object and function pointers can be passed as a predicate to an algorithm, but function objects are also adaptable and increase the scope, flexibility, and efficiency of the C++ Standard Library. If, for example, a value needed to be bound to a function before being passed to an algorithm, then a function pointer could not be used. Function adaptors convert function pointers into adaptable function objects that can be bound to a value. The header \<functional> also contains member function adaptors that allow member functions to be called as adaptable function objects. Functions are adaptable if they have nested type declarations specifying their argument and return types. The C++ Standard requires that this adaptability is implemented by having all standard object classes inherit from the unary_function or binary_function base classes. Function objects and their adaptors allow the C++ Standard Library to upgrade existing applications and help integrate the library into the C++ programming environment.
2121

22-
The Visual C++ implementation of the function objects in \<functional> includes *transparent operator functors*, which are specializations of standard function objects and take no template parameters, and perform perfect forwarding of the function arguments and perfect return of the result. This feature is part of the C++14 Draft Standard specification. These template specializations do not require that you specify argument types when you invoke arithmetic, comparison, logical, and bitwise operator functors. You can overload arithmetic, comparison, logical, or bitwise operators for your own types, or for heterogeneous combinations of types, and then use the transparent operator functors as function arguments. For example, if your type *MyType* implements `operator<`, you can call `sort(my_collection.begin(), my_collection.end(), less<>())` instead of explicitly specifying the type `sort(my_collection.begin(), my_collection.end(), less<MyType>())`.
22+
The Visual C++ implementation of the function objects in \<functional> includes *transparent operator functors*. which are specializations of standard function objects and take no template parameters, and perform perfect forwarding of the function arguments and perfect return of the result. This feature is part of the C++14 Draft Standard specification. These template specializations do not require that you specify argument types when you invoke arithmetic, comparison, logical, and bitwise operator functors. You can overload arithmetic, comparison, logical, or bitwise operators for your own types, or for heterogeneous combinations of types, and then use the transparent operator functors as function arguments. For example, if your type *MyType* implements `operator<`, you can call `sort(my_collection.begin(), my_collection.end(), less<>())` instead of explicitly specifying the type `sort(my_collection.begin(), my_collection.end(), less<MyType>())`.
2323

2424
## C++11/C++14 Implementation
2525

@@ -68,7 +68,7 @@ Every call wrapper has a move constructor and a copy constructor. A *simple call
6868
|Class|Description|
6969
|-|-|
7070
|[bad_function_call](../standard-library/bad-function-call-class.md)|A class that describes an exception thrown to indicate that a call to `operator()` on a [function](../standard-library/function-class.md) object failed because the object was empty.|
71-
|[binary_negate](../standard-library/binary-negate-class.md)|A template class providing a member function that negates the return value of a specified binary function.|
71+
|[binary_negate](../standard-library/binary-negate-class.md)|A template class providing a member function that negates the return value of a specified binary function. Deprecated in C++17. |
7272
|[binder1st](../standard-library/binder1st-class.md)|A template class providing a constructor that converts a binary function object into a unary function object by binding the first argument of the binary function to a specified value.|
7373
|[binder2nd](../standard-library/binder2nd-class.md)|A template class providing a constructor that converts a binary function object into a unary function object by binding the second argument of the binary function to a specified value.|
7474
|[const_mem_fun_ref_t](../standard-library/const-mem-fun-ref-t-class.md)|An adapter class that allows a const member function that takes no arguments to be called as a unary function object when initialized with a reference argument.|
@@ -86,7 +86,7 @@ Every call wrapper has a move constructor and a copy constructor. A *simple call
8686
|[pointer_to_binary_function](../standard-library/pointer-to-binary-function-class.md)|Converts a binary function pointer into an adaptable binary function.|
8787
|[pointer_to_unary_function](../standard-library/pointer-to-unary-function-class.md)|Converts a unary function pointer into an adaptable unary function.|
8888
|[reference_wrapper](../standard-library/reference-wrapper-class.md)|A class that wraps a reference.|
89-
|[unary_negate](../standard-library/unary-negate-class.md)|A template class providing a member function that negates the return value of a specified unary function.|
89+
|[unary_negate](../standard-library/unary-negate-class.md)|A template class providing a member function that negates the return value of a specified unary function. Deprecated in C++17. |
9090

9191
### Functions
9292

@@ -103,8 +103,9 @@ Every call wrapper has a move constructor and a copy constructor. A *simple call
103103
|[mem_fn](../standard-library/functional-functions.md#mem_fn)|Generates a simple call wrapper.|
104104
|[mem_fun](../standard-library/functional-functions.md#mem_fun)|Helper template functions used to construct function object adaptors for member functions when initialized with pointer arguments.|
105105
|[mem_fun_ref](../standard-library/functional-functions.md#mem_fun_ref)|A helper template function used to construct function object adaptors for member functions when initialized with reference arguments.|
106-
|[not1](../standard-library/functional-functions.md#not1)|Returns the complement of a unary predicate.|
107-
|[not2](../standard-library/functional-functions.md#not2)|Returns the complement of a binary predicate.|
106+
|[not1](../standard-library/functional-functions.md#not1)|Returns the complement of a unary predicate. Deprecated in C++17. |
107+
|[not2](../standard-library/functional-functions.md#not2)|Returns the complement of a binary predicate. Deprecated in C++17. |
108+
|[not_fn](../standard-library/functional-functions.md#not_fn)|Returns the complement of the result of its function object. New in C++17. |
108109
|[ptr_fun](../standard-library/functional-functions.md#ptr_fun)|A helper template function used to convert unary and binary function pointers, respectively, into unary and binary adaptable functions.|
109110
|[ref](../standard-library/functional-functions.md#ref)|Constructs a `reference_wrapper` from an argument.|
110111
|[swap](../standard-library/functional-functions.md#swap)|Swaps two `function` objects.|

0 commit comments

Comments
 (0)