Every examples of member template that I have seen be it in the C++ Template book or in Mr. Stroustrup book consist of templatized copy constructors or assignment operators where the template argument can be deduced from the member function parameter. I wanted to write a member template with no function argument and I could not find in both mentionned books how such function could be called. Here is an example:
class A { public : /* How do you call doX() ? */ template <int ID> doX() {} };
I have found out by experimenting that it is the exact same syntax than for function template:
A a;
a.doX<1>();
I have written to the authors of the C++ Template book to point out this omission in their book and here is what David Vandervoorde has nicely answered:
Hello,
Thanks for the feedback -- I think I agree.
Do not forget section 9.3.3 ("Dependent Names of Templates") in C++ Template, which may apply in cases like this. E.g., using your example as a basis:
template<typename T> void f(T x) { x.template doX<1>(); }Without that extra "template" keyword, the code would be invalid.
Cheers,
Daveed
Comments are closed for this post.
I want you to find in this blog informations about C++ programming that I had a hard time to find in the first place on the web.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
<< < | > >> | |||||
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |