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. I've tried explicit template instantiation, (by passing a flag to my compiler), but in that case, i've to do that for every class suppose i try to invoke std::vector <someOtherClass> someVector; I've to instantiate this also explicitly. (which is a problem for me, because, I use a lot of standard library, and its a pain for me to instantiate every time. Also, if i do the above, i can always instantiate myClass also like this. template class myClass <someOtherClass> ; and which i want to avoid this.. to summarize my question, is there any way i can restrict the compiler to accept only few type names for my templated class (i.e., using standard c++ or boost) Thanks, Surya
Gennadiy Rozental wrote:
is there any way i can restrict the compiler to accept only few type names for my templated class (i.e., using standard c++ or boost)
I believe boost::enable_if is used for this purpose.
Gennadiy
I think, BOOST_STATIC_ASSERT is another way. Let me correct if i'm wrong. Thanks, Surya
"Suki"
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>
{};
Imperative way:
#include
to summarize my question, is there any way i can restrict the compiler to accept only few type names for my templated class (i.e., using standard c++ or boost)
HTH, -- Dave Abrahams Boost Consulting www.boost-consulting.com
Imperative way:
#include
#include #include template <class T> struct myClass { BOOST_MPL_ASSERT((boost::mpl::or_< boost::is_same
, boost::is_same , boost::is_same >)); ... // your class implementation here. };
to summarize my question, is there any way i can restrict the compiler to accept only few type names for my templated class (i.e., using standard c++ or boost)
HTH,
Ah.. Thank you, I was just thinking in the same lines. May be i should go over mpl more thoroughly. Surya
David Abrahams wrote:
"Suki"
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
#include #include template <class T> struct myClass { BOOST_MPL_ASSERT((boost::mpl::or_< boost::is_same
, boost::is_same , boost::is_same >)); ... // 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
participants (4)
-
David Abrahams
-
Gennadiy Rozental
-
Simon Buchan
-
Surya Kiran Gullapalli