Home
Fractals
Tutorials
Books
Archive
My blog
My LinkedIn Profile

BOOKS i'm reading

Cryptography engineering, Niels Ferguson, Bruce Schneier, Tadayoshi Kohno, ISBN: 9780470474242
Advanced Programming in the UNIX(R) Environment (2nd Edition), W. Richard Stevens, Stephen A. Rago, ISBN:0201433079
Trading For a Living, Alexander Elder, ISBN:0471592242

mailto:olivier@olivierlanglois.net

Archives for: May 2007, 30

05/30/07

Permalink 04:07:09 pm, by lano1106 Email , 198 words, 538 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

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.

May 2007
Sun Mon Tue Wed Thu Fri Sat
 << < Current> >>
    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

XML Feeds

What is RSS?

Who's Online?

  • Guest Users: 6

powered by
b2evolution