Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Curious use of default template argument?

Posted on 2012-09-05
3
Medium Priority
?
387 Views
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
{
public:
	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?

Thanks.
0
Comment
Question by:mrwad99
  • 2
3 Comments
 
LVL 22

Accepted Solution

by:
ambience earned 1000 total points
ID: 38372835
The quick and rough answer is that within CMyTemplateClass1, "CMyTemplateClass1" refers to the instantiation of CMyTemplateClass1<T>

So

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.
0
 
LVL 22

Expert Comment

by:ambience
ID: 38373511
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 {
public:
   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 }

[Example:
template<class T> class X
{
   X* p; // meaning X<T>
   X<T>* p2;
   X<int>* p3;
};
—end example]
0
 
LVL 19

Author Closing Comment

by:mrwad99
ID: 38381182
Excellent answer, thank you very much :)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

572 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