Why does the language not admit this syntax?

If I have an instance of a class C, named foo:

C foo;

And I want the address of a member function void SetValue(int);

Why doesn't C++ consider this valid:

auto pFn = &foo.SetValue;

After all, if there is a member variable int m_count;, then auto pMemVar = &foo.m_count; is considered valid...
foo.SetValue is an expression of a very special category, a pending member function call expression. It can only be used with the function call operator, and in no other manner (ยง5.2.5[expr.ref]/4)

To construct a member function pointer, the only valid syntax is &C::SetValue, and the underlying reason is that each instance of C, such as foo, has its own foo.m_count, whose address can be taken, but the SetValue is the same between all instances of C, except where virtual. Note that &C::m_count is not the same thing as &foo.m_count
Last edited on
Thanks for the clarification. I guess I need to find where one can suggest a change to the standard. Unless there is a strong reason for this, it seems anti-intuitive and creates some nasty syntax when combined with auto-keyword.

see: http://stackoverflow.com/questions/15416305/take-the-address-of-a-member-function-without-referring-to-the-instances-class
I guess I need to find where one can suggest a change to the standard.

http://isocpp.org/std has links.
Last edited on
Topic archived. No new replies allowed.