﻿<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<style type="text/css">
pre {margin-left:20pt; }
pre > i {
  font-family: "OCR A Extended", "Consolas", "Lucida Console", monospace;
  font-style:italic;
}
code > i {
  font-family: "OCR A Extended", "Consolas", "Lucida Console", monospace;
  font-style:italic;
}
pre > em {
  font-family: "OCR A Extended", "Consolas", "Lucida Console", monospace;
  font-style:italic;
}
code > em {
  font-family: "OCR A Extended", "Consolas", "Lucida Console", monospace;
  font-style:italic;
}
body { color: #000000; background-color: #FFFFFF; }
del { text-decoration: line-through; color: #8B0040; }
ins { text-decoration: underline; color: #005100; }

p.example { margin-left: 2em; }
pre.example { margin-left: 2em; }
div.example { margin-left: 2em; }

code.extract { background-color: #F5F6A2; }
pre.extract { margin-left: 2em; background-color: #F5F6A2;
  border: 1px solid #E1E28E; }

p.function { }
.attribute { margin-left: 2em; }
.attribute dt { float: left; font-style: italic;
  padding-right: 1ex; }
.attribute dd { margin-left: 0em; }

blockquote.std { color: #000000; background-color: #F1F1F1;
  border: 1px solid #D1D1D1;
  padding-left: 0.5em; padding-right: 0.5em; }
blockquote.stddel { text-decoration: line-through;
  color: #000000; background-color: #FFEBFF;
  border: 1px solid #ECD7EC;
  padding-left: 0.5empadding-right: 0.5em; ; }

blockquote.stdins { text-decoration: underline;
  color: #000000; background-color: #C8FFC8;
  border: 1px solid #B3EBB3; padding: 0.5em; }

table.header { border: 0px; border-spacing: 0;
  margin-left: 0px; font-style: normal; }

table { border: 1px solid black; border-spacing: 0px;
  margin-left: auto; margin-right: auto; }
th { text-align: left; vertical-align: top;
  padding-left: 0.4em; border: none; 
  padding-right: 0.4em; border: none; }
td { text-align: left; vertical-align: top;
  padding-left: 0.4em; border: none;
  padding-right: 0.4em; border: none; }
</style>

<title>A proposal to add a generalized callable negator (Revision 1)</title>
</head>

<body>

<table class="header"><tbody>
  <tr>
    <th>Document number:&nbsp;&nbsp;</th><th> </th><td>N3800</td>
  </tr>
  <tr>
    <th>Date:&nbsp;&nbsp;</th><th> </th><td>2013-09-16</td>
  </tr>
  <tr>
    <th>Project:&nbsp;&nbsp;</th><th> </th><td>Programming Language C++, Library Working Group</td>
  </tr>
  <tr>
    <th>Reply-to:&nbsp;&nbsp;</th><th> </th><td><address>Tomasz Kamiński &lt;tomaszkam at gmail dot com&gt;</address></td>
  </tr>
</tbody></table>

<h1><a name="title">A proposal to add a generalized callable negator (Revision 1)</a></h1>

<h2><a name="intro">Introduction</a></h2>

<p>This proposal is to add function template <code>not_fn</code> that will allow to create negator of any callable object.</p>

<!--h2><a name="toc">Table of contents</a></h2-->

<h2><a name='revision'>Revision history</a></h2>

<p>Changes since <a href='http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3699.html'>N3699</a>:</p>
<ul>
  <li>Change the requirements of <code>not_fn</code> to allow wrapping of move-only types.</code>
  <li>Added missing Throws clause.</li>
  <li>Included changes to 20.10 [function.objects]/2 section.</code>
  <li>Corrected link to "Generic (Polymorphic) Lambda Expressions" proposal.</code>
  <li>Changed formating of the document.</li>
</ul>

<h2><a name="motivation">Motivation and Scope</a></h2>

<p>The standard negators <code>not1</code> and <code>not2</code> accept only unary and binary functors
   that define <code>argument_type</code> or <code>first_argument_type</code> and <code>second_argument_type</code> respectively,
   which make them unusable with results of standard library functions such as <code>bind</code> and <code>mem_fn</code>.
   Furthermore, with relation to <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3421.htm">N3421</a>,
   they cannot be used with new operator functor specializations.</p>

<p>This proposal addresses the problem by introducing a template function <code>not_fn</code> that returns complement of arbitrary predicate.</p>

<h3><a name="motivation.lambda">Comparison with lambda</a></h3>

<p>While the polymorphic lambda expressions (<a href="http://isocpp.org/files/papers/N3649.html">N3649</a>) may be used to negate arbitrary functor,
   the proposed function offers more compact syntax:</p>
<pre>  std::partition(v.begin(), v.end(), [f](auto&amp; p) { return !f(p); });
  std::partition(v.begin(), v.end(), std::not_fn(f));
</pre>

<p>Furthermore, the use of lambda expression requires separate treatment of member pointers:</p>
<pre>  std::partition(v.begin(), v.end(), [pm](auto&amp; p) { return !std::mem_fn(pm)(p); });
  std::partition(v.begin(), v.end(), std::not_fn(pm));
</pre>

<h3><a name="motivation.depraction">Deprecation</a></h3>
<p>With the incorporation of proposed functionality the old standard negators should be deprecated.</p>

<h2><a name="design">Design Decisions</a></h2>

<h3><a name="design.specializations">New specializations of <code>unary_negate</code>, <code>binary_negate</code></a></h3>

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

<h3><a name="design.return">Return type</a></h3>

<p>The perfect forwarding of return type was chosen instead of hard-coded <code>bool</code>.
   Similar argumentation to the one provided in <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3421.htm">N3421</a> applies.</p>

<h3><a name="design.return">Argument typedefs</a></h3>

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

<h2><a name="standard">Impact On The Standard</a></h2>

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

<p>Nothing depends on this proposal.</p>

<h2><a name="wording">Proposed wording</a></h2>

<p>After the declaration of <code>not2</code> in the section 20.10 [function.objects]/2 (Header <code>&lt;functional&gt;</code> synopsis), add:</p>

<blockquote class="stdins"> 

<pre>  // 20.10.9, <em>negators</em>
  template &lt;class F&gt; <em>unspecified</em> not_fn(F&amp;&amp; f);
</pre>

</blockquote>


<p>After paragraph 20.10.8 Negators [negators], insert a new paragraph. (Chapter [bind] (Function template <code>bind</code>) becomes 20.10.?)</p>
  
<blockquote class="stdins"> 
<h4><a name="optional.general">20.10.9 Function template <code>not_fn</code> <span style="float:right">[not_fn]</span></a></h4>

<pre>  template &lt;class F&gt;
    <em>unspecified</em> not_fn(F&amp;&amp; f);
</pre>

  <dl class="attribute">

    <dd><p>In the text that follows, the following names have the following meanings:
    </p><ul>
      <li><code>FD</code> is the type <code>decay&lt;F&gt;::type</code>,</li>
      <li><code>fd</code> is an lvalue of type <code>FD</code> constructed from <code>std::forward&lt;F&gt;(f),</code>
      <li><code>fn</code> is a simple call wrapper created as a result of <code>not_fn(f)</code>,</li>
      <li><code>FN</code> is a type of <code>fn</code>,</li>
      <li><code>FW</code> is a type of forwarding call wrapper for <code>f</code> with a weak result type ([func.require] 20.10.2).</li>
    </ul><p></p></dd>

    <dt>Requires:</dt> 
    <dd></code>is_constructible&lt;FD, F&gt;::value</code> shall be <code>true</code>. 
         <code>f</code> shall be a callable object ([func.require] 20.10.2).</p></dd>

    <dt>Returns:</dt>
    <dd><p>A simple call wrapper <code>fn</code> such that the expression <code>fn(a1, a2, ..., aN)</code> 
           is equivalent to <code>!<em>INVOKE</em>(fd, a1, a2, ..., aN)</code> ([func.require] 20.10.2).</p></dd>

    <dt>Throws:</dt>
    <dd><p>Nothing unless the construction of <code>fd</code> throws an exception.</p></dd>

    <dt>Remarks:</dt>
    <dd><p>The return type shall satisfy the requirements of <code>MoveConstructible</code>.
        If <code>FD</code> satisfy the requirements of <code>CopyConstructible</code>, then 
        the return type shall satisfy the requirements of <code>CopyConstructible</code>.
        [ <em>Note:</em> This implies that <code>FD</code> is </code>MoveConstructible.</code>
          <em> — end note</em> ]</p></dd>


    </dd><dd><p>The <code>FN</code> shall define a nested type named <code>result_type</code> as a synonym to <code>R</code>
                only if <code>FW</code> would have a nested type <code>result_type</code>; 
                the type <code>R</code> is <code>decltype(!declval&lt;typename FW::result_type&gt;())</code>.</p></dd>

    <dd><p>The <code>FN</code> shall define a nested type named <code>argument_type</code> as a synonym for <code>T1</code> 
           only if the type <code>F</code> is any of the following:</p></dd>
    <dd><ul>
       <li>a function type or a pointer to function type taking one argument of type <code>T1</code></li> 
       <li>a pointer to member function <code>R T0::f() <em>cv</em></code> (where <code><em>cv</em></code> represents the member function's cv-qualifiers);
           the type <code>T1</code> is <code><em>cv</em> T0*</code></li>
       <li>a class type with a member type <code>argument_type</code>;
           the type <code>T1</code> is <code>F::argument_type.</code>.</li>
    </ul><p></p></dd>
    
    <dd><p>The <code>FN</code> shall define two nested types named <code>first_argument_type</code> and <code>second_argument_type</code> 
           as synonyms for <code>T1</code> and <code>T2</code>, respectively, only if the type <code>F</code> is any of the following:</p></dd>
    <dd><ul>
       <li>a function type or a pointer to function type taking two arguments of types <code>T1</code> and <code>T2</code></li> 
       <li>a pointer to member function <code>R T0::f(T2) <em>cv</em></code> (where <code><em>cv</em></code> represents the member function's cv-qualifiers);
           the type <code>T1</code> is <code><em>cv</em> T0*</code></li>
       <li>a class type with member types <code>first_argument_type</code> and <code>second_argument_type</code>;
           the type <code>T1</code> is <code>F::first_argument_type</code> and the type <code>T2</code> is <code>F::second_argument_type.</code></li>
    </ul><p></p></dd>

  </dl>
</blockquote>

<h2><a name="acknowledgements">Acknowledgements</a></h2>

<p>Anna Salwa originally proposed <code>not_fn</code> in discussion group <a href="https://groups.google.com/a/isocpp.org/forum/?fromgroups#!topic/std-proposals/ki4-jb5WzhA">ISO C++ Standard - Future Proposals</a>.</p>
<p>Mateusz Kwiatkowski, Jonathan Wakely and Daniel Krügler offered many useful suggestions and corrections to the proposal.</p>

<h2><a name="literature">References</a></h2>

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

</body>
</html>
