Document number: |  | N4076
Date: |  | 2014-06-17
Project: |  | Programming Language C++, Library Working Group
Reply-to: |  |

# A proposal to add a generalized callable negator (Revision 4)

## Introduction

This proposal is to add function template `not_fn` that will allow to create negator of any callable object.

## Revision history

Changes since N4022:

- Changed the paragraph numbers to match newest working draft.
- Changed the requires clause to apply to constructed functor instead of function argument.
- Updated wording for `argument_type`, `first_argument_type` and `second_argument_type` to match resolution of LWG issue #2387.
- Changed definition of `fn` to forwarding call wrapper.
- Replaced `decay<F>::type` with `decay_t<F>`.
- Corrected spelling mistakes in Motivation and Scope section.
- Removed nested typedefs and added note, based on LWG review.

Changes since N3800:

- Updated proposed wording per LWG guidance.

Changes since N3699:

- Changed the requirements of `not_fn` to allow wrapping of move-only types.
- Added missing Throws clause.
- Included changes to 20.9 [function.objects]/2 section.
- Corrected link to "Generic (Polymorphic) Lambda Expressions" proposal.
- Changed formating of the document.

## Motivation and Scope

The standard negators `not1` and `not2` accept only unary and binary functors that define `argument_type` or `first_argument_type` and `second_argument_type` respectively, which make them unusable with results of standard library functions such as `bind` and `mem_fn`. Furthermore, with relation to N3421, they cannot be used with new operator functor specializations.

This proposal addresses the problem by introducing a template function `not_fn` that returns complement of arbitrary predicate.

### Comparison with lambda

While the polymorphic lambda expressions (N3649) may be used to negate arbitrary functor, the proposed function offers more compact syntax:

      std::partition(v.begin(), v.end(), [f](auto& p) { return !f(p); });
      std::partition(v.begin(), v.end(), std::not_fn(f));

Furthermore, the use of lambda expression requires separate treatment of member pointers:

      std::partition(v.begin(), v.end(), [pm](auto& p) { return !std::mem_fn(pm)(p); });
      std::partition(v.begin(), v.end(), std::not_fn(pm));

### Deprecation

With the incorporation of proposed functionality the old standard negators should be deprecated.

## Design Decisions

### New specializations of `unary_negate`, `binary_negate`

Problem addressed by this paper may be solved by introducing perfect forwarding specializations of `unary_negate` and `binary_negate` for types that do not define `argument_type` and `first_argument_type`, `second_argument_type` nested types respectively, without breaking existing code. Although this solution does not address functions with arbitrary number of arguments and requires additional implementation burden.

### Return type

The perfect forwarding of return type was chosen instead of hard-coded `bool`. Similar argumentation to the one provided in N3421 applies.

In Rapperswil decided not to define this type.

### Argument typedefs

The `argument_type`, `first_argument_type` and `second_argument_type` nested types are required by implementation to be consistent with most of library functors, that define these types where they are well defined for target callable type. Furthermore it is increasing the amount of existing code that will remain valid after changing `not1`, `not2` to `not_fn`.

In Rapperswil decided not to define these types.

## Impact On The Standard

This proposal has no dependencies beyond a C++11 compiler and Standard Library implementation. (It depends on perfect forwarding, `decltype` and trailing return types.)

Nothing depends on this proposal.

## Proposed wording

After the declaration of `not2` in the section 20.9 [function.objects]/2 (Header `<functional>` synopsis), add:

>       // 20.9.9, negators
>       template <class F> unspecified not_fn(F&& f);

After paragraph 20.9.8 Negators [negators], insert a new paragraph. (Chapter [bind] (Function template `bind`) becomes 20.9.?)

> #### 20.9.9 Function template `not_fn` [not_fn]

>       template <class F>
>         unspecified not_fn(F&& f);

>   In the text that follows:

>   - `FD` is the type `decay_t<F>`,
>   - `fd` is an lvalue of type `FD` constructed from `std::forward<F>(f),`
>   - `fn` is a forwarding call wrapper created as a result of `not_fn(f)`,

> Requires:
>   `is_constructible<FD, F>::value` shall be `true`. `fd` shall be a callable object ([func.def] 20.9.1).

> Returns:

>   A forwarding call wrapper `fn` such that the expression `fn(a1, a2, ..., aN)` is equivalent to `!INVOKE(fd, a1, a2, ..., aN)` ([func.require] 20.9.2).

> Throws:

>   Nothing unless the construction of `fd` throws an exception.

> Remarks:

>   The return type shall satisfy the requirements of `MoveConstructible`. If `FD` satisfies the requirements of `CopyConstructible`, then the return type shall satisfy the requirements of `CopyConstructible`. [ Note: This implies that `FD` is MoveConstructible. — end note ]

>   [ Note: Function template `not_fn` can usually provide a better solution than using the negators `not1` and `not2`. — end note ]

## Acknowledgements

Anna Salwa originally proposed `not_fn` in discussion group ISO C++ Standard - Future Proposals.

Mateusz Kwiatkowski, Jonathan Wakely and Daniel Krügler offered many useful suggestions and corrections to the proposal.

## References

1. Stephan T. Lavavej, "Making Operator Functors greater<>" (N3421, http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3421.htm)
2. Faisal Vali, Herb Sutter, Dave Abrahams, "Generic (Polymorphic) Lambda Expressions (Revision 3) " (N3649, http://isocpp.org/files/papers/N3649.html)