[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

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

Peng Yu

10/25/2008 2:54:00 AM

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;
}
3 Answers

Kai-Uwe Bux

10/25/2008 11:01:00 AM

0

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

annamalai

10/26/2008 3:29:00 PM

0

On Oct 24, 9:54 pm, Peng Yu <PengYu...@gmail.com> 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::operator() 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...

Thomas J. Gritzan

10/26/2008 3:52:00 PM

0

> On Oct 24, 9:54 pm, Peng Yu <PengYu...@gmail.com> 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-lite/templates.html...

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

--
Thomas