 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
srik Guest
|
Posted: Sun Jun 26, 2005 2:10 pm Post subject: Specialization using default template parameter |
|
|
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
|
Posted: Mon Jun 27, 2005 10:42 am Post subject: Re: Specialization using default template parameter |
|
|
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
|
Posted: Mon Jun 27, 2005 10:44 am Post subject: Re: Specialization using default template parameter |
|
|
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
|
Posted: Tue Jun 28, 2005 9:40 am Post subject: Re: Specialization using default template parameter |
|
|
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
|
Posted: Wed Jun 29, 2005 1:58 pm Post subject: Re: Specialization using default template parameter |
|
|
"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 |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|