Home
Fractals
Tutorials
Books
My blog
My LinkedIn Profile

BOOKS i'm reading

Napoleon Hill Keys to Success: The 17 Principles of Personal Achievement, Napoleon Hill, ISBN: 978-0452272811
The 4-Hour Workweek: Escape 9-5, Live Anywhere, and Join the New Rich (Expanded and Updated), Timothy Ferriss, ISBN: 978-0307465351
The Fountainhead, Ayn Rand, ISBN: 0452273331
Web Hosting Canada

mailto:olivier@olivierlanglois.net

C++ Member templates and template arguments

05/30/07

Permalink 04:07:09 pm, by lano1106, 198 words, 2260 views   English (CA)
Categories: C++

C++ Member templates and template arguments

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, Pingbacks:

Comment from: lano1106 [Member]
I just found out that the section C.13.6
(Template as a Qualifier) of the book "The C++ programming language"
of Bjarne Stroustrup explains address this exact issue.
PermalinkPermalink 09/14/07 @ 19:08

Comments are closed for this post.

Olivier Langlois's blog

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.

October 2024
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 31    

Search

Custom Search

Misc

XML Feeds

What is RSS?

Who's Online?

  • Guest Users: 7

powered by
b2evolution