Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to use the template member function of a template in the memberfunction of another template class?

Reply
Thread Tools

How to use the template member function of a template in the memberfunction of another template class?

 
 
Peng Yu
Guest
Posts: n/a
 
      10-25-2008
Hi,

I have the following program. The error line is commented. I want to
call _a's member function 'doit' with the template argument of
function<T>. Would you please what is the correct way to do it?

Thanks,
Peng

#include <iostream>

template <typename T>
struct function {
T operator()() const {
return T();
}
};

template <typename T>
struct A {
template <typename F>
T doit() {
return F()();
}
};

template <typename T>
struct B {
public:
T doit() {
return _a.doit<function<T> >();//error
}
private:
A<T> _a;
};


int main() {
A<double> a;
std::cout << a.doit<function<double> >() << std::endl;
}
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      10-25-2008
Peng Yu wrote:

> I have the following program. The error line is commented. I want to
> call _a's member function 'doit' with the template argument of
> function<T>.

[snip]
>
> #include <iostream>
>
> template <typename T>
> struct function {
> T operator()() const {
> return T();
> }
> };
>
> template <typename T>
> struct A {
> template <typename F>
> T doit() {
> return F()();
> }
> };
>
> template <typename T>
> struct B {
> public:
> T doit() {
> return _a.doit<function<T> >();//error


Try:

return _a.template doit< function<T> >();

> }
> private:
> A<T> _a;
> };
>
>
> int main() {
> A<double> a;
> std::cout << a.doit<function<double> >() << std::endl;
> }



Best

Kai-Uwe Bux

 
Reply With Quote
 
 
 
 
annamalai
Guest
Posts: n/a
 
      10-26-2008
On Oct 24, 9:54 pm, Peng Yu <(E-Mail Removed)> wrote:
> template <typename T>
> struct B {
> public:
> T doit() {
> return _a.doit<function<T> >();//error
> }
> private:
> A<T> _a;
>
> };


The only way I was able to make it work was to make the B::doit() as a
template member function. I don't know enough to explain why the
compiler is not able to instantiate function<T> as used above. Maybe
someone with more knowledge will be able to explain it. Here is the
complete program.

#include <iostream>

template <typename T>
struct function {
T operator()() const {
std::cout << "T function:perator() called" << std::endl;
return T();
}

};

template <typename T>
struct A {
template <typename F>
T doit() {
std::cout << "T A::doit() called" << std::endl;
return F()();
}

};

template <typename T>
struct B {
public:
template <typename P>
T doit() {
std::cout << "T B::doit() called" << std::endl;
return _a.doit<P>();
}
private:
A<T> _a;

};

int main() {
A<double> a;
double d = a.doit< function<double> >();
B<double> b;
double e = b.doit< function<double> >();
}

--
Copyright of Wedding Album Must Belong to Family (The Producers)
http://tinyurl.com/d0253082-copyright
 
Reply With Quote
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      10-26-2008
> On Oct 24, 9:54 pm, Peng Yu <(E-Mail Removed)> wrote:
>> template <typename T>
>> struct B {
>> public:
>> T doit() {
>> return _a.doit<function<T> >();//error


doit is a dependent name, so you have to tell the compiler, when it is a
typename or template. Otherwise, the compiler assumes a non-typename and
non-template member. Insert the template keyword before the function name:

return _a.template doit<function<T> >();

The FAQ explains this for the typename keyword, but it also applies to
templates:
http://www.parashift.com/c++-faq-lit...html#faq-35.18

>> }
>> private:
>> A<T> _a;
>>
>> };


--
Thomas
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Member function pointer to member function of another class Praetorian C++ 11 04-03-2008 08:33 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
cool thing with memberfunction pointers as parameters PKH C++ 2 05-27-2004 07:48 PM
Passing a pointer to member function as a parameter to another member function Newsgroup - Ann C++ 5 07-30-2003 02:54 AM



Advertisments