|
Boost Users : |
From: Simon Buchan (simon_at_[hidden])
Date: 2005-10-30 19:00:57
David Abrahams wrote:
> "Suki" <suryakiran.gullapalli_at_[hidden]> writes:
>
>> Hi,
>> I'm writing a templated class, and i dont want to use the class
>> otherthan for some predetermined types, say, int, double etc.
>>
>> This class has no meaning for typenames other than those few.
>>
>> =========================
>> template <class T>
>> myClass {....} ;
>>
>> myClass<int> intClass ;
>> myClass<float> floatClass ;
>> myClass<char> charClass ;
>> =========================
>>
>> The compiler should not complain for the first 2 instantiations, but
>> should bail out at the 3rd instantiation (charClass). Is there any way
>> i
>> can achieve this, using standard c++ or boost.
>
> Declarative way:
>
> template <class T>
> struct myClass_impl
> {
> ...
> };
>
> template <class T>
> struct myClass;
>
> template<>
> myClass<int> : myClass_impl<int>
> {};
>
> template<>
> myClass<float> : myClass_impl<float>
> {};
>
> template<>
> myClass<char> : myClass_impl<char>
> {};
>
you mean 'struct myClass<foo> : public myClass_impl<foo>', I assume?
> Imperative way:
>
> #include <boost/mpl/or.hpp>
> #include <boost/mpl/assert.hpp>
> #include <boost/type_traits/is_same.hpp>
>
> template <class T>
> struct myClass
> {
> BOOST_MPL_ASSERT((boost::mpl::or_<
> boost::is_same<T,int>
> , boost::is_same<T,float>
> , boost::is_same<T,char>
> >));
>
> ... // your class implementation here.
> };
>
This would slow down compile times a bit, though, would it not? (but
give better error messages!) Not that it matters much.
Where's the functional way? :-D (Don't answer that)
Perhaps abstracting the check to a traits class would be helpful?
Depends on what it's for, I guess.
-- don't quote this
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net