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

P

Peng Yu

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;
}
 
K

Kai-Uwe Bux

Peng said:
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 said:
}
private:
A<T> _a;
};


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


Best

Kai-Uwe Bux
 
A

annamalai

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

Thomas J. Gritzan

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-lite/templates.html#faq-35.18
 

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

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top