Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

13 views
Skip to first unread message

Juha Nieminen

unread,
Jan 26, 2010, 3:09:55 PM1/26/10
to
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?

Pete Becker

unread,
Jan 26, 2010, 4:37:42 PM1/26/10
to

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.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of
"The Standard C++ Library Extensions: a Tutorial and Reference"
(www.petebecker.com/tr1book)

Juha Nieminen

unread,
Jan 27, 2010, 12:52:50 PM1/27/10
to
Pete Becker <pe...@versatilecoding.com> wrote:
> 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?

Pete Becker

unread,
Jan 27, 2010, 2:26:09 PM1/27/10
to

It wasn't designed to. ptr_fun was the approach in the original STL.

0 new messages