C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Specialization using default template parameter

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
srik
Guest





PostPosted: Sun Jun 26, 2005 2:10 pm    Post subject: Specialization using default template parameter Reply with quote



Consider this class template with 'int' as default parameter

template<typename T=int> class Q { // ---------- (1)
public:
Q() {cout << "main" << endl;}
};

// specialization using 'int'
template<> class Q<int> { // ----------- (2)
public:
Q() { cout << "specialized"<< endl; }
};

The instantiations
Q Q<> q2; // invokes (2)

(2) being the most specialized, it is considered but
I thought the template is already implicitly specialized for 'int'
and the second one should be treated as a redefinition. Why not?

srik


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Greg
Guest





PostPosted: Mon Jun 27, 2005 10:42 am    Post subject: Re: Specialization using default template parameter Reply with quote





srik wrote:
Quote:
Consider this class template with 'int' as default parameter

template<typename T=int> class Q { // ---------- (1)
public:
Q() {cout << "main" << endl;}
};

// specialization using 'int'
template<> class Q<int> { // ----------- (2)
public:
Q() { cout << "specialized"<< endl; }
};

The instantiations
Q Q<> q2; // invokes (2)

(2) being the most specialized, it is considered but
I thought the template is already implicitly specialized for 'int'
and the second one should be treated as a redefinition. Why not?

srik


The presence of a default parameter type in a template declaration is
analagous to the presence of a default parameter in a function
declaration. Both allow the programmer to use a shorthand notation to
instantiate the template or call the function in the source code, but
in neither case is template instantiated or function called any
different than if the default parameter did not exist and the
programmer had to provide the parameter on their own.

In other words:

template<typename T=int>
class Q
{
public:
Q() {cout << "main" << endl;}
};

does not specialize template Q for type int because no such
specialization would be created if "=int" were removed from Q's
declaration.

Greg


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ismail Pazarbasi
Guest





PostPosted: Mon Jun 27, 2005 10:44 am    Post subject: Re: Specialization using default template parameter Reply with quote



It's not treated as redefinition; it's a specialized form of the
template and it's the best match. Compiler uses the best match. In (2),
you already specified int explicitly. Compiler should not generate code
for (1), as far as I know, therefore there should not be any
conflict/ambiguity between (1) and (2).

Ismail


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
srik
Guest





PostPosted: Tue Jun 28, 2005 9:40 am    Post subject: Re: Specialization using default template parameter Reply with quote

Greg wrote:
Quote:
srik wrote:
Consider this class template with 'int' as default parameter

template<typename T=int> class Q { // ---------- (1)
public:
Q() {cout << "main" << endl;}
};

// specialization using 'int'
template<> class Q<int> { // ----------- (2)
public:
Q() { cout << "specialized"<< endl; }
};


....
In other words:

template class Q
{
public:
Q() {cout << "main" << endl;}
};

does not specialize template Q for type int because no such
specialization would be created if "=int" were removed from Q's
declaration.

If "=int" were removed, following declaration will be in error.
Q<> q2; // too few arguments for class template

It looks odd that it requires default parameter to be defined
for a class template it doesn't use.
It uses the specialization instead.

Second, if the default parameter is replaced by a different type,
say "char", q2 would silently revert to the main template.

srik


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Fraser Ross
Guest





PostPosted: Wed Jun 29, 2005 1:58 pm    Post subject: Re: Specialization using default template parameter Reply with quote

"srik"
Quote:
Greg wrote:
template<typename T=int
class Q
{
public:
Q() {cout << "main" << endl;}
};

does not specialize template Q for type int because no such
specialization would be created if "=int" were removed from Q's
declaration.
The default argument has nothing to do with the specialisation definition.


Quote:

If "=int" were removed, following declaration will be in error.
Q<> q2; // too few arguments for class template

It looks odd that it requires default parameter to be defined
for a class template it doesn't use.
It uses the specialization instead.
int is a default argument. The class template can be instantiated using the

default argument. The most specialised class template is chosen.


Fraser.




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.