Curious use of default template argument?

Posted on 2012-09-05
Last Modified: 2012-09-09
Ah hello.

I came across code similar to the following, and was confused about why it compiled.  Please consider the following:

template<class T>class Class2{public: T m_t;};
template<class T>class Class3{};

template<class T>
class CMyTemplateClass1
	typedef Class2<CMyTemplateClass1> myT;	// Compiles
	typedef Class2<Class3> myT2;		// Does not compile

main(int argc, char * argv[])
	CMyTemplateClass1<char>::myT t;
	CMyTemplateClass1<char> tty = t.m_t; // **

Open in new window

(Sorry it is not exactly the most readable:))

Now, the first typedef compiles, whereas I thought it would fail since the template argument type, CMyTemplateClass1, requires its own template argument type.  The second typedef, of exactly the same nature, fails to compile for this reason.

The second line in main(), marked **, shows that the template argument that has been taken in the first typedef is that which we setup CMyTemplateClass to have: char.

My question is simple.  Why is this happening, and is it standard?

Question by:mrwad99
    LVL 22

    Accepted Solution

    The quick and rough answer is that within CMyTemplateClass1, "CMyTemplateClass1" refers to the instantiation of CMyTemplateClass1<T>


    typedef Class2<CMyTemplateClass1> myT;

    is actually

    typedef Class2< CMyTemplateClass1<T> > myT;

    which is an instantiated class, unlike

    typedef Class2<Class3> myT2;

    where Class3 is a template and not a concrete class.

    As regards standard, yes it is standard. I'll try to find where in the standard this behavior is specified.
    LVL 22

    Expert Comment

    From the standard:

    14.6.1 Locally declared names [temp.local]

    1 Within the scope of a class template, when the name of the template is neither qualified nor followed by <, it is equivalent to the name of the template followed by the template parameters enclosed in <>.

    [Example: the constructor for Set can be referred to as Set() or Set<T>().]
    template<class T> class Set {
       Set<T>(const Set<T>&);

    Other specializations of the class can be referred to by explicitly qualifying the template name with the appropriate template arguments. { see p3 in following example }

    template<class T> class X
       X* p; // meaning X<T>
       X<T>* p2;
       X<int>* p3;
    —end example]
    LVL 19

    Author Closing Comment

    Excellent answer, thank you very much :)

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
    Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now