std::not1() doesn't accept a function pointer

J

Juha Nieminen

Basically any STL algorithm which requires a predicate as parameter
can be given either a function object or a regular function as that
parameter. For example, you can give a function object or a regular
function pointer as the third parameter to std::remove_if(), and it
will work ok.

If you want to *negate* the predicate, you can do it with std::not1().
However, for some reason std::not1() can only be called with a function
object, not a function pointer. If you want to use it with a regular
function, you have to do it like std::not1(std::ptr_fun(theFunction)).

But why? Why couldn't std::not1() accept a regular function as parameter?
 
J

Juha Nieminen

Pete Becker said:
Because it has to return an object whose type has a couple of nested
typedefs, and their definitions are propagated from the type that you
instantiate it with. A pointer to function doesn't have those typedefs,
so can't be used here. The same problem arises with not2, bind1st and
bind2nd. The solution is to wrap the function pointer in a type that
provides those typedefs, using ptr_fun.

Why cannot std::not1() do this itself? Why do you have to do it
manually?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top