Re: currying pointer to member functions
On 2013-03-07 09:27, Ulrich Eckhardt wrote:
Am 06.03.2013 00:20, schrieb Jerry:
I appreciate any advice about how to do this.
I can make this work:
struct a
{
int b;
int c() {return b+1;}
int d(int x) {return b+x;}
};
int main()
{
a m = { 1 }, n = { 2 };
a *ps = &m;
int (a::*pf)() = &a::c;
std::cout << (ps->*pf)() << std::endl;
return 0;
}
And it runs the function and everything works. But what I want to
do is curry the function so that I can store (ps->*pf) and then
later execute it. So what is the type of &(ps->*pf) ?
I'd rephrase that: What is the type of "ps->*pf"?
Actually it is possible to derive a answer for that question (albeit
more as an academical game). According to 13.6 p11
"For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type,
C1 is the same type as C2 or is a derived class of C2, T is an object
type or a function type, and CV1 and CV2 are cv-qualifier-seqs, there
exist candidate operator functions of the form
CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
where CV12 is the union of CV1 and CV2."
In this example CV1, CV2, and CV12 are empty, C1 and C2 are both equal
to type 'a', and T is equal to the type 'int()'. In this case we could
argue that the return type of "ps->*pf" is 'int(&)()'. The problem is
here that the wording constraints of 5.5 p6:
"If the result of .* or ->* is a function, then that result can be
used only as the operand for the function call operator ()."
does not give use much freedom to take advantage of that result.
Also similar, what is they type of "m.d"?
This is a little bit different, because the core language specification
does not even give us a hint about such a type for "m.d". In the case of
expressions of the form 'a->*b' or 'a.*b' a answer is possible (but not
helpful).
It's a bit like taking the address of an overloaded function, which
you can do but which requires you to static_cast it to one of the
overloads to disambiguate, because it doesn't have a type or value
itself otherwise.
This comparison doesn't fit well. There exists indeed an *unambiguous*
answer.
HTH & 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! ]