Pointers to member functions and default parameters

D

Dave

Hello all,

At the line marked "Problem here???" below, I get successful compilation on
one platform and failure on another. What does the Standard say about this?
Is this a correct program? May a member function be called through a
pointer when some parameter default values are accepted (i.e. not all
parameters are passed)?

Thanks,
Dave


#include <iostream>

using namespace std;

class foo
{
public:
int do_it(int = 10) {return 42;}
};

template <typename RET, typename OBJECT_TYPE, typename PTR_TO_MEM_FUN>
RET call_it(OBJECT_TYPE &ds, PTR_TO_MEM_FUN pm)
{
return (ds.*pm)(); // Problem here???
}

int main()
{
foo bar;

cout << call_it<int>(bar, &foo::do_it) << endl;
}
 
C

Christopher Benson-Manica

Dave said:
#include <iostream>
using namespace std;
class foo
{
public:
int do_it(int = 10) {return 42;}
};
template <typename RET, typename OBJECT_TYPE, typename PTR_TO_MEM_FUN>
RET call_it(OBJECT_TYPE &ds, PTR_TO_MEM_FUN pm)
{
return (ds.*pm)(); // Problem here???
^^^^ I don't think this is legal :)
int main()
{
foo bar;
cout << call_it<int>(bar, &foo::do_it) << endl;
}

(I'm not an authority; any of the below could be [really?] wrong!)

Well, among other things, you didn't complete the template
specification (I'm sure that's the wrong word...) - the template takes
three arguments, and you only supplied one. Another thing is that
template arguments should be object types, and a pointer to a member
function doesn't sound like it belongs there.
 
D

Dave

Christopher Benson-Manica said:
Dave said:
#include <iostream>
using namespace std;
class foo
{
public:
int do_it(int = 10) {return 42;}
};
template <typename RET, typename OBJECT_TYPE, typename PTR_TO_MEM_FUN>
RET call_it(OBJECT_TYPE &ds, PTR_TO_MEM_FUN pm)
{
return (ds.*pm)(); // Problem here???
^^^^ I don't think this is legal :)
int main()
{
foo bar;
cout << call_it<int>(bar, &foo::do_it) << endl;
}

(I'm not an authority; any of the below could be [really?] wrong!)

Well, among other things, you didn't complete the template
specification (I'm sure that's the wrong word...) - the template takes
three arguments, and you only supplied one. Another thing is that
template arguments should be object types, and a pointer to a member
function doesn't sound like it belongs there.

The other two template parameters are deduced by the compiler.

A template type parameter does not have to be of class type. It may be of a
built-in type, an enumeration, a pointer-to-member, etc...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top