By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,414 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,414 IT Pros & Developers. It's quick & easy.

pointer to member / strange construction

P: n/a
Can someone explain the following construct to me:
What is not clear to me is what the argument to foo is.
A pointer to a member?

template <class U>
static char foo(void (U::*)(void));

regards,
alex
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
What looks strange to me is the U::* part. No function name, only
a class qualifier? Could you please give more detail or tell me, where to
look up in the standard.
template <class U>
static char foo(void (U::*)(void));


Yes, it's a pointer to a member function of U. The function
takes no arguments and returns nothing. You could use it
this way:

struct realU {
void bar();
};
...
char c = foo(&realU::bar);

(the compiler should be able to derive 'U == realU' here)

Victor


Alex
Jul 19 '05 #2

P: n/a
"Alexander Stippler" <st**@mathematik.uni-ulm.de> wrote...
What looks strange to me is the U::* part. No function name, only
a class qualifier? Could you please give more detail or tell me, where to
look up in the standard.
Please don't top-post.

The declaration of a formal argument does not require a name.
If I were to declare a stand-alone variable, I'd give it a name:

void (MyClass::*ptrToMemOfMyClass)(); // name is required

if the same declaration (of a pointer to a member) would happen
to be part of a function declaration, you could drop the name:

void foo( void (MyClass::*)() ); // foo is a function
^^^^^^^^^^^^^^^^^^^

As to the asterisk, are you familiar with the syntax

"<type-id> (*)(<arg-list>)"?

Example:

void foo(int (*)(double, char));
^^^^^^^^^^^^^^^^^^^^^

? In the declaration above the argument 'foo' takes is
a pointer to a function that takes two arguments and returns
int. Now, the only difference with a member function is that
it is prefixed with the class name and the scope resolution
operator:

void foo(int (MyClass::*)(double, char));

which means that the pointer is NOT to a regular function, but
to a non-static member function of MyClass.

The formal argument name is optional when declaring a function,
so the next two declarations are equivalent:

int foo(double d, char c);
int foo(double, char);

The same with pointers to functions:

int foo(int (*myfuncptr)(double,char));
int foo(int (*)(double,char));

('myfuncptr' is the name of the formal argument).

Relevant sections of the Standard include 8.3.5, 8.3.1, 8.3.3
(and all they reference).
template <class U>
static char foo(void (U::*)(void));


Yes, it's a pointer to a member function of U. The function
takes no arguments and returns nothing. You could use it
this way:

struct realU {
void bar();
};
...
char c = foo(&realU::bar);

(the compiler should be able to derive 'U == realU' here)

Victor


Alex


Victor
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.