 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
@velu.invalid Guest
|
Posted: Tue Jun 28, 2005 10:40 am Post subject: placing restrictions in template class creation |
|
|
Hi ,
Can i restrict the types for which the class can be created.
say I have a generic class Cfoo and i would like to create only
Cfoo<int> , Cfoo<float> & i dont want to allow Cfoo<double> ,
Is there any way to place a constraint like this ?
Regds,
@velu
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Stanimir Kabaivanov Guest
|
Posted: Tue Jun 28, 2005 12:05 pm Post subject: Re: placing restrictions in template class creation |
|
|
Hi Velu,
| Quote: | Can i restrict the types for which the class can be created.
say I have a generic class Cfoo and i would like to create only
Cfoo<int> , Cfoo<float> & i dont want to allow Cfoo<double> ,
Is there any way to place a constraint like this ?
Yes, you can place restriction on your template types, and its a common |
question what you're asking.
I'd recommend to read Bjarne Stroustrup technical FAQ, since this is
explained very well there:
http://www.research.att.com/~bs/bs_faq2.html
Check out "Why can't I define constraints for my template parameters?"
answer (don't worry - you can ).
In particular the example provided in the FAQ for Can_copy template will
work with your float, int and double example. But at least it will
produce compiler warning for precission loss.
Hope this one helps,
Best regards,
Stanimir Kabaivanov
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Tue Jun 28, 2005 12:07 pm Post subject: Re: placing restrictions in template class creation |
|
|
In article <1119948377.943106.83220 (AT) g49g2000cwa (DOT) googlegroups.com>,
<""@velu".invalid"> wrote:
| Quote: | Hi ,
Can i restrict the types for which the class can be created.
say I have a generic class Cfoo and i would like to create only
Cfoo<int> , Cfoo<float> & i dont want to allow Cfoo<double> ,
Is there any way to place a constraint like this ?
Regds,
@velu
simplest if classes are not big,,, |
template <class T> struct foo_base;
template <> struct foo_base<int> {};
template <> struct foo_base<float>{};
template <class T>
class foo:foo_base<T>
{
//....
};
see boost::enable_if
and boost::mpl for a general solution...
template <class T>
class foo:boost::enable_if
<
boost::mpl::contains
<
boost::mpl::vector
T
| Quote: | ,
mpl::identity
::type
{ |
//...
}
come to mind not tested!!!
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Tue Jun 28, 2005 1:10 pm Post subject: Re: placing restrictions in template class creation |
|
|
In article <1119948377.943106.83220 (AT) g49g2000cwa (DOT) googlegroups.com>,
""@velu".invalid" <thirumalaivelu (AT) gmail (DOT) com> writes
| Quote: | Hi ,
Can i restrict the types for which the class can be created.
say I have a generic class Cfoo and i would like to create only
Cfoo<int> , Cfoo<float> & i dont want to allow Cfoo<double> ,
Is there any way to place a constraint like this ?
|
Declare the template but do not define it. Now provide specialisations
for the types you want to use.
For those that are curious, the only way of 'overloading' a class name
is via a class template.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
chris jefferson Guest
|
Posted: Tue Jun 28, 2005 3:52 pm Post subject: Re: placing restrictions in template class creation |
|
|
"@velu".invalid wrote:
| Quote: | Hi ,
Can i restrict the types for which the class can be created.
say I have a generic class Cfoo and i would like to create only
Cfoo<int> , Cfoo<float> & i dont want to allow Cfoo<double> ,
Is there any way to place a constraint like this ?
My personal favourite way to do something like that is like: |
template<class T>
struct is_valid_for_class
{
static const int value = false;
};
template<class T, bool check = is_valid_for_class
struct my_class;
template<class T>
struct my_class<T,true>
{ ... }
Then overload is_valid_for_class for each type where it is valid.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
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
|
|