472,961 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,961 software developers and data experts.

pointer to member function as template argument

Hi all,

I've run into something confusing on MS VC6. Yeah I know it's old but
that's what the client wants, so...

I'm trying to pass a pointer to a member function as a template
argument, and the compiler gives me an invalid template argument on the
member function address if the member function returns a type. A
member function with a void return type is fine. The example below
demonstrates the problem:

class SomeClass
{
int FuncWithReturn(){return 0;}
void VoidFunc(int arg){};
};

template TemplateRetFunc<class T, class RetType, RetType (T::*F)() >
{
};

template TemplateVoidFunc<class T, class Arg, void (T::*F)(Arg) >
{
};

int main()
{
TemplateRetFunc<SomeClass, int, &SomeClass::FuncWithReturn> test1;
//error
TemplateVoidFunc<SomeClass, int, &SomeClass::VoidFunc> test2; //OK
return 0;
}

Anyone know why having the return type gives an error? I'm fine if I
have to use a void return, I just want to know why.

Damien

Damien

Apr 18 '06 #1
4 7301
Damien wrote:
I've run into something confusing on MS VC6. Yeah I know it's old but
that's what the client wants, so...

I'm trying to pass a pointer to a member function as a template
argument, and the compiler gives me an invalid template argument on
the member function address if the member function returns a type. A
member function with a void return type is fine. The example below
demonstrates the problem:

class SomeClass
{
int FuncWithReturn(){return 0;}
void VoidFunc(int arg){};
};

template TemplateRetFunc<class T, class RetType, RetType (T::*F)() >
{
};

template TemplateVoidFunc<class T, class Arg, void (T::*F)(Arg) >
{
};

int main()
{
TemplateRetFunc<SomeClass, int, &SomeClass::FuncWithReturn> test1;
//error
TemplateVoidFunc<SomeClass, int, &SomeClass::VoidFunc> test2; //OK
return 0;
}

Anyone know why having the return type gives an error? I'm fine if I
have to use a void return, I just want to know why.


VC6 is really bad when it comes to templates. Try to convince your client
to upgrade or to change the compiler.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 18 '06 #2
Damien wrote:
Hi all,

I've run into something confusing on MS VC6. Yeah I know it's old but
that's what the client wants, so...

I'm trying to pass a pointer to a member function as a template
argument, and the compiler gives me an invalid template argument on the
member function address if the member function returns a type. A
member function with a void return type is fine. The example below
demonstrates the problem:

class SomeClass
{
public:
int FuncWithReturn(){return 0;}
void VoidFunc(int arg){};
};

template TemplateRetFunc<class T, class RetType, RetType (T::*F)() >
Syntax error!
{
};

template TemplateVoidFunc<class T, class Arg, void (T::*F)(Arg) >
{
};

int main()
{
TemplateRetFunc<SomeClass, int, &SomeClass::FuncWithReturn> test1;
//error
TemplateVoidFunc<SomeClass, int, &SomeClass::VoidFunc> test2; //OK
return 0;
}

Anyone know why having the return type gives an error? I'm fine if I
have to use a void return, I just want to know why.


Compiler bug/non-compliance issue. VC6 has lots of them.

Tom
Apr 18 '06 #3
Oh, this is embarrassing. In my haste to throw together a non-client
example I wrote utter rubbish instead of valid C++. Note to self:
Always compile examples, even little ones. Should have been:

class SomeClass
{
public:
int FuncWithReturn(){return 0;}
void VoidFunc(int arg){};
};

template <class T, class RetType, RetType (T::*F)() > class
TemplateRetFunc
{
};

template <class T, class Arg, void (T::*F)(Arg)> class TemplateVoidFunc
{
};

int main()
{
TemplateRetFunc<SomeClass, int, &SomeClass::FuncWithReturn> test1;
//error
TemplateVoidFunc<SomeClass, int, &SomeClass::VoidFunc> test2; //OK
return 0;
}

Which is fine under gcc, but barfs on VC6. Might have to encourage an
upgrade.

Sorry for wasting your time on stupid code.

Damien

Apr 18 '06 #4

Damien wrote:
Hi all,

I've run into something confusing on MS VC6. Yeah I know it's old but
that's what the client wants, so...

I'm trying to pass a pointer to a member function as a template
argument, and the compiler gives me an invalid template argument on the
member function address if the member function returns a type. A
member function with a void return type is fine. The example below
demonstrates the problem:

class SomeClass
{
int FuncWithReturn(){return 0;}
void VoidFunc(int arg){};
};

template TemplateRetFunc<class T, class RetType, RetType (T::*F)() >
{
};

template TemplateVoidFunc<class T, class Arg, void (T::*F)(Arg) >
{
};

int main()
{
TemplateRetFunc<SomeClass, int, &SomeClass::FuncWithReturn> test1;
//error
TemplateVoidFunc<SomeClass, int, &SomeClass::VoidFunc> test2; //OK
return 0;
}

Anyone know why having the return type gives an error? I'm fine if I
have to use a void return, I just want to know why.

Damien

Damien


vc6.0sp4 fails to compile the following perfectly valid C++ code as
well. Try a better compiler.

#include <vector>
#include <iostream>

template<typename T>
class MyArray {
public:
MyArray();
~MyArray();
void append(const T& item);
void display();
private:
std::vector<T> *v;

};

template<typename T>
MyArray<T>::MyArray() {
v = new std::vector<T>();

}

template<typename T>
MyArray<T>::~MyArray() {
delete v;

}

template<typename T>
void MyArray<T>::append(const T& item) {
v->push_back(item);

}

template<typename T>
void MyArray<T>::display() {

typename std::vector<T>::iterator ivi;
for(ivi = v->begin(); ivi != v->end(); ++ivi)
std::cout << *ivi << std::endl;

}

int main(int argc, char** argv) {

MyArray<std::string> ma1;
ma1.append("Test1");
ma1.append("Test2");

ma1.display();
}

Apr 20 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Alexander Stippler | last post by:
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)); ...
3
by: jimjim | last post by:
Hello, My question concerns as to how a pointer is passed by reference as a function argument. The following is from code taken from the MICO implementation of the CORBA specification. in...
4
by: Gert Van den Eynde | last post by:
Hi all, A beginners question.... I've got a template class template <class T> classA {...} In an other class, I want to pass a pointer to an instance of classA as a function argument....
15
by: Albert | last post by:
Hi, I need to pass a pointer-to-member-function as a parameter to a function which takes pointer-to-function as an argument. Is there any way to do it besides overloading the function? Here...
4
by: firegun9 | last post by:
Hello everyone, here is my program: /////////////////////////////// #include <iostream> using namespace std; void multi(double* arrayPtr, int len){ for(int i=0; i<len; i++)...
0
by: ivan.leben | last post by:
I am writing this in a new thread to alert that I found a solution to the problem mentioned here: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/7970afaa089fd5b8 and to avoid...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
1
by: autumn | last post by:
Hi everybody, I'm having problem passing pointer to member object as template argument, seems VC 2005 does not allow 'pointer to base member' to 'pointer to derived member' conversion in template...
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.