473,385 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

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<doublea;
std::cout << a.doit<function<double() << std::endl;
}
Oct 25 '08 #1
3 2015
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<doublea;
std::cout << a.doit<function<double() << std::endl;
}

Best

Kai-Uwe Bux

Oct 25 '08 #2
On Oct 24, 9:54 pm, Peng Yu <PengYu...@gmail.comwrote:
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<Tas 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<doublea;
double d = a.doit< function<double();
B<doubleb;
double e = b.doit< function<double();
}

--
Copyright of Wedding Album Must Belong to Family (The Producers)
http://tinyurl.com/d0253082-copyright
Oct 26 '08 #3
On Oct 24, 9:54 pm, Peng Yu <PengYu...@gmail.comwrote:
>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
Oct 26 '08 #4

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

Similar topics

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...
7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
2
by: Dominik Fritz | last post by:
Hi, I have a template class which contains a nested class. The .cpp file is included at the end of the header file. If I write the implementation of a member function of my nested class in this...
2
by: Gert Van den Eynde | last post by:
Hi all, I have a template class template<typename TC> class A{ public: ... protected: TC ObjTC; }
2
by: pkpatil | last post by:
Hi, Can a private composite object in a class access private or protected members of base class? For e.g. class composite { void memberFunction(); };
3
by: Curten | last post by:
Hi, I'm a beginner and need help with the following: I have defined a class A where a private data member is an array of structs; private: vector<a_struct> Data; The values of the...
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...
2
by: pascal.zschumme | last post by:
hello folks My problem is that the following code using something very very difficult technique :D fails to compile on MSVC8: <code> // main.cpp // // the error is: // main.cpp(8) : fatal...
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.