Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > pointer to member function as template argument

Reply
Thread Tools

pointer to member function as template argument

 
 
Damien
Guest
Posts: n/a
 
      04-18-2006
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-18-2006
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


 
Reply With Quote
 
 
 
 
Tom Widmer
Guest
Posts: n/a
 
      04-18-2006
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
 
Reply With Quote
 
Damien
Guest
Posts: n/a
 
      04-18-2006
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

 
Reply With Quote
 
Fei Liu
Guest
Posts: n/a
 
      04-20-2006

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();
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function pointer as template argument - does not work for function template avasilev C++ 4 12-22-2011 02:11 PM
Passing pointer to template function as argument to pointer to template function Vijai Kalyan C++ 4 11-08-2005 07:53 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM
design question - member function argument or pointer member Gert Van den Eynde C++ 8 01-26-2004 05:20 PM



Advertisments