Thread: Nested template

  1. #16
    Join Date
    Oct 2007
    Inside my computer
    Quote Originally Posted by laserlight View Post
    By the way, note that MSVC2005 is MSVC8.
    I figured, but it's easier to say MSVC2005 since it's the actual product name.

    Although, now that you think about it, the compiler does parse the header, and if a template class, or something else the compiler doesn't know about when it parses it, it might need a little help.
    I'm suspecting it needs typename because when it first parses the header, it doesn't know the type, and so you will have to tell it it's a type, pretty much like the compiler doesn't know about external variables.
    I think we can agree to, then, that if they wanted to, typename isn't required, but they did make it to ease it for compiler or something, so it can parse the header directly instead of doing a pass when it knows the type of T.

  2. #17
    and the hat of sweating
    Join Date
    Aug 2007
    Toronto, ON
    Oh crap! I think I might have just figured out why typename is required...
    After re-reading the error produced by the following code:
    #include <iostream>
    #include <list>
    using namespace std;
    template <class T>
    class A { };
    template <class T>
    class B {
            B() { std::list< A< T > >::const_iterator i; }
    int main(void) {
            B<int> a();
            return 0;
    My compiler is gcc 4.1.3, and the error is

    test.cpp: In constructor B<T>::B():
    test.cpp:11: error: expected `;' before i
    It looks like the compiler (by default) assumes that A::B is referring to a member variable in A, but then it sees 'i' right after and doesn't know what to do with it, kind of like this example:
    int a;
    a i;
    So typename is required to override the default assumption of the compiler.

    Hey cool, I think I just answered my own question.

  3. #18
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Here is a rather contrived example that shows just how big a difference it can make.

    template <typename T>
    void foo()
      int bar(T::foobar());
    If typename wasn't necessary, what would the function body do?

    Answer: if T::foobar is a type, it would declare a function called bar that returns an int and takes a parameter of type T::foobar with no name.
    If T::foobar is a variable that is callable, it would define a local variable called bar of type int and initialize it with the result of calling T::foobar.
    Quite different, isn't it?

    (Note that under no circumstances, the above code would define a local variable called bar of type int and initialize it with a default-constructed temporary object of type T::foobar. That's an ambiguity in the C++ language syntax that is resolved as, "Everything that can be a function prototype, is." To make it do that, you have to do:
    int bar((T::foobar()));
    The additional set of parentheses shows that it cannot be a formal parameter.)
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template class nested in template class
    By yahn in forum C++ Programming
    Replies: 7
    Last Post: 04-18-2009, 11:40 AM
  2. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM