Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make negate_unary work with any type?
    text
    copied!<p>Following this question: <a href="https://stackoverflow.com/questions/4583310/how-to-negate-a-predicate-function-using-operator-in-c">How to negate a predicate function using operator ! in C++?</a><br> I want to create an operator ! can work with any functor that inherited from unary_function. I tried:</p> <pre><code>template&lt;typename T&gt; inline std::unary_negate&lt;T&gt; operator !( const T&amp; pred ) { return std::not1( pred ); } </code></pre> <p>The compiler complained:</p> <pre><code>Error 5 error C2955: 'std::unary_function' : use of class template requires template argument list c:\program files\microsoft visual studio 10.0\vc\include\xfunctional 223 1 Graphic Error 7 error C2451: conditional expression of type 'std::unary_negate&lt;_Fn1&gt;' is illegal c:\program files\microsoft visual studio 10.0\vc\include\ostream 529 1 Graphic Error 3 error C2146: syntax error : missing ',' before identifier 'argument_type' c:\program files\microsoft visual studio 10.0\vc\include\xfunctional 222 1 Graphic Error 4 error C2065: 'argument_type' : undeclared identifier c:\program files\microsoft visual studio 10.0\vc\include\xfunctional 222 1 Graphic Error 2 error C2039: 'argument_type' : is not a member of 'std::basic_ostream&lt;_Elem,_Traits&gt;::sentry' c:\program files\microsoft visual studio 10.0\vc\include\xfunctional 222 1 Graphic Error 6 error C2039: 'argument_type' : is not a member of 'std::basic_ostream&lt;_Elem,_Traits&gt;::sentry' c:\program files\microsoft visual studio 10.0\vc\include\xfunctional 230 1 Graphic </code></pre> <p>Any idea? </p> <p><strong>Update</strong><br> Follow "templatetypedef" solution, I got new error: </p> <pre><code>Error 3 error C2831: 'operator !' cannot have default parameters c:\visual studio 2010 projects\graphic\graphic\main.cpp 39 1 Graphic Error 2 error C2808: unary 'operator !' has too many formal parameters c:\visual studio 2010 projects\graphic\graphic\main.cpp 39 1 Graphic Error 4 error C2675: unary '!' : 'is_prime' does not define this operator or a conversion to a type acceptable to the predefined operator c:\visual studio 2010 projects\graphic\graphic\main.cpp 52 1 Graphic </code></pre> <p><strong>Update 1</strong><br> Complete code:</p> <pre><code>#include &lt;iostream&gt; #include &lt;functional&gt; #include &lt;utility&gt; #include &lt;cmath&gt; #include &lt;algorithm&gt; #include &lt;iterator&gt; #include &lt;string&gt; #include &lt;boost/assign.hpp&gt; #include &lt;boost/assign/std/vector.hpp&gt; #include &lt;boost/assign/std/map.hpp&gt; #include &lt;boost/assign/std/set.hpp&gt; #include &lt;boost/assign/std/list.hpp&gt; #include &lt;boost/assign/std/stack.hpp&gt; #include &lt;boost/assign/std/deque.hpp&gt; struct is_prime : std::unary_function&lt;int, bool&gt; { bool operator()( int n ) const { if( n &lt; 2 ) return 0; if( n == 2 || n == 3 ) return 1; if( n % 2 == 0 || n % 3 == 0 ) return 0; int upper_bound = std::sqrt( static_cast&lt;double&gt;( n ) ); for( int pf = 5, step = 2; pf &lt;= upper_bound; ) { if( n % pf == 0 ) return 0; pf += step; step = 6 - step; } return 1; } }; /* template&lt;typename T&gt; inline std::unary_negate&lt;T&gt; operator !( const T&amp; pred, typename T::argument_type* dummy = 0 ) { return std::not1&lt;T&gt;( pred ); } */ inline std::unary_negate&lt;is_prime&gt; operator !( const is_prime&amp; pred ) { return std::not1( pred ); } template&lt;typename T&gt; inline void print_con( const T&amp; con, const std::string&amp; ms = "", const std::string&amp; sep = ", " ) { std::cout &lt;&lt; ms &lt;&lt; '\n'; std::copy( con.begin(), con.end(), std::ostream_iterator&lt;typename T::value_type&gt;( std::cout, sep.c_str() ) ); std::cout &lt;&lt; "\n\n"; } int main() { using namespace boost::assign; std::vector&lt;int&gt; nums; nums += 1, 3, 5, 7, 9; nums.erase( remove_if( nums.begin(), nums.end(), !is_prime() ), nums.end() ); print_con( nums, "After remove all primes" ); } </code></pre> <p>Thanks,<br> Chan Nguyen</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload