1 Title: C++ template WTF
2 Tags: en, d, c++, template, wtf, programming
4 See this small program::
15 this->foo_A<int>(); // line 10
25 You may think it should compile. Well, it doesn't::
28 t.cpp: In member function ‘void B<T>::foo_B()’:
29 t.cpp:10: error: expected primary-expression before ‘int’
30 t.cpp:10: error: expected ‘;’ before ‘int’
32 Today I've learned a new (**horrible**) *feature* of C++, ``foo_A`` is an
33 ambiguous__ symbol for C++. I've seen the ``typename`` keyword being used to
34 disambiguate types before (specially when using iterators) but never a template.
35 Here is the code that works::
46 this->template foo_A<int>();
48 // or: A<T>::template foo_A<int>();
49 // but not simply: template foo_A<int>();
59 __ http://womble.decadent.org.uk/c++/template-faq.html#disambiguation
61 Note how you have to help the compiler, explicitly saying *yes, believe me,
62 foo_A is a template* because it has no clue. Also note that the ``template``
63 keyword is only needed when ``A``, ``B`` and ``A::foo_A`` are all templates;
64 remove the ``template<...>`` to any of them, and the original example will
65 compile flawlessly, so this is a **special special special** case.
69 In D__ things are more natural, because templates are not ambiguous (thanks to
70 the odd ``symbol!(Type)`` syntax), you can just write::
87 __ http://digitalmars.com/d/
89 And all works as expected.
92 .. vim: set et sw=3 sts=3 :