Re: mem_fun and smart pointers?
On 6 Mrz., 14:56, SG <s.gesem...@gmail.com> wrote:
On 5 Mrz., 20:28, "Hak...@gmail.com" wrote:
[..]
Is there an entirely different or
more correct way to do this? Is there a library addition (not language
feature, like lambdas) in C++0x that does what i expect?
There is a std::mem_fn function in the C++0x standard library but it
doesn't look like it supports user-defined pointer-like objects. It's
basically defined in terms of INVOKE ( 20.8.2) which doesn't mention
anything like that.
It is correct, that the wording doesn't mention
this situation directly, but the bulleted list
in [func.require]/1 does support this use-case
indirectly.
The definition of INVOKE starts with these two
items:
1 Define INVOKE(f, t1, t2, ..., tN) as follows:
? (t1.*f)(t2, ..., tN) when f is a pointer to a
member function of a class T and t1 is an object
of type T or a reference to an object of type T
or a reference to an object of a type derived
from T;
? ((*t1).*f)(t2, ..., tN) when f is a pointer to
a member function of a class T and t1 is not
one of the types described in the previous item;
[..]
The first bullet does not apply here, because
t1 is a reference to shared_ptr<A>, and T is
A, not being related to shared_ptr<A> by
inheritance.
The description of the second bullet makes
pretty clear ("t1 is not one of the types
described in the previous item") that this
is a logical switch which ends the decision
sequence of the remaining bullet list items.
The question is: Is the expression "*t1"
wellformed? It turns out that it is
well-formed, because shared_ptr<A> provides
an operator* overload that returns A&,
which binds perfectly to the remaining
parts of the INVOKE expression.
This analysis is confirmed by the fact
that gcc 4.4.1-2 likes the OP's code with
mem_fun replaced by mem_fn.
Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]