| View previous topic :: View next topic |
| Author |
Message |
wong.kam.keung@gmail.com Guest
|
Posted: Wed Oct 12, 2005 12:57 am Post subject: 'Unspecialize' template? |
|
|
Hi all,
I want to remove a template specialization by using another template to
match the specialized template. Is it possible?
template <typename T>
struct remove_ptr;
template <typename T>
struct remove_ptr<T*>
{
typedef T type; // <-- removed point to T
};
template
struct remove_specialize;
template <typename T, typename X>
struct remove_specialize<T // <-- compile error
{
typedef T type;
};
Thanks.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Greg Herlihy Guest
|
Posted: Wed Oct 12, 2005 4:37 pm Post subject: Re: 'Unspecialize' template? |
|
|
[email]wong.kam.keung (AT) gmail (DOT) com[/email] wrote:
| Quote: | Hi all,
I want to remove a template specialization by using another template to
match the specialized template. Is it possible?
|
It is possible to use a template to break apart a template
specialization into the class template and the parameterized type (see
below).
| Quote: | template <typename T
struct remove_ptr;
template
struct remove_ptr
{
typedef T type; // <-- removed point to T
};
template
struct remove_specialize;
template
struct remove_specialize // <-- compile error
{
typedef T type;
};
|
To accomplish the first step (separating the class template from its
specialized type) requires a class template that accepts a template
template parameter:
template
struct remove_specialization
{
typedef X specialized_type; // ok
typedef T type; // Error - T is a template not a type
};
But as this example shows, it is not possible to accomplish the second
step and create a typedef for the class template T. Only types can be
typedef-ed, and a class template, unlike its specializations, is not a
type. So I would re-examine how remove_specialization is meant to be
used.
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
wong.kam.keung@gmail.com Guest
|
Posted: Wed Oct 12, 2005 10:50 pm Post subject: Re: 'Unspecialize' template? |
|
|
Thanks for clearing this up.
I tried your suggestion and lets say I want to re-specialize a single
parameter template into a different type:
template <template
struct replace_specialization
{
typedef ST<Y> type;
};
template <class T>
struct add_pointer
{
typedef T* type;
};
....
{
replace_specialization<add_pointer; // <-- Error:
Expecting a class template
replace_specialization
}
I got an error when I provide a specialized template.
BTW, is the class X in your original example suppose to be in the inner
template's parameter list?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Greg Herlihy Guest
|
Posted: Thu Oct 13, 2005 1:02 pm Post subject: Re: 'Unspecialize' template? |
|
|
[email]wong.kam.keung (AT) gmail (DOT) com[/email] wrote:
| Quote: | Thanks for clearing this up.
I tried your suggestion and lets say I want to re-specialize a single
parameter template into a different type:
template <template
struct replace_specialization
{
typedef ST<Y> type;
};
template <class T
struct add_pointer
{
typedef T* type;
};
...
{
replace_specialization; // <-- Error:
Expecting a class template
replace_specialization
}
I got an error when I provide a specialized template.
|
Yes, I was a little careless with the example template that I provided.
I apologize for conflating what should actually be two declarations:
one for the class template (which is left undefined) and one for the
specialization.
Here is the "improved" (i.e. tested) version of Remove_Specialization:
template
struct Remove_Specialization;
template <template
struct Remove_Specialization< T
{
typedef X specialized_type;
};
and here is a variation to "change" the specialized type of a template
class:
#include <string>
#include <vector>
template <class T, class U>
struct Change_Specialization;
template <template
struct Change_Specialization< T
{
typedef T<U> type;
};
// and some test declarations...
// IntV is a vector of ints type
typedef std::vector<int> IntV;
// use Change_Specialization to change a vector of ints
// type into a vector of strings type
typedef Change_Specialization< IntV, std::string>::type StringV;
// instantiate a vector of strings to be sure the change worked
StringV stringVector;
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|