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 

placing restrictions in template class creation

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





PostPosted: Tue Jun 28, 2005 10:40 am    Post subject: placing restrictions in template class creation Reply with 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


[ 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





PostPosted: Tue Jun 28, 2005 12:05 pm    Post subject: Re: placing restrictions in template class creation Reply with quote



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 Wink ).

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





PostPosted: Tue Jun 28, 2005 12:07 pm    Post subject: Re: placing restrictions in template class creation Reply with quote



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





PostPosted: Tue Jun 28, 2005 1:10 pm    Post subject: Re: placing restrictions in template class creation Reply with quote

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





PostPosted: Tue Jun 28, 2005 3:52 pm    Post subject: Re: placing restrictions in template class creation Reply with quote

"@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
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.