class template question
I assume you mean in C++.
If a type doesn't support an arithmetic operation and the container requires it, it won't compile. Explicitly restricting the types a container can operate on, beyond what is functionally required of them, would limit the flexibility of your continer, which could cause you grief later.
Edit: Just to clarify, when I say 'requires it', I mean they are used somewhere in the containers definition, not enforced by some arbitrary restriction.
[Edited by - Liam M on July 22, 2009 8:31:59 PM]
If a type doesn't support an arithmetic operation and the container requires it, it won't compile. Explicitly restricting the types a container can operate on, beyond what is functionally required of them, would limit the flexibility of your continer, which could cause you grief later.
Edit: Just to clarify, when I say 'requires it', I mean they are used somewhere in the containers definition, not enforced by some arbitrary restriction.
[Edited by - Liam M on July 22, 2009 8:31:59 PM]
I assume you've got a template like below, and you want it to have a compile error if someone uses a type (for T) that is not a number type?Is this a correct assumption?
template<class T>class MyNumber{ T number;};
Actually, come to think of it, you can do it using template specialisation, like so:
But once again, don't box yourself in needlessly.
template <class T> class MyClass{public: MyClass();}; template <> MyClass<int>::MyClass(){ //Constructor junk.}int main(){ CTest<int> t; //this is OK, constructor is defined. CTest<float> t; //This isn't, no constructor is defined for float. return 0;}
But once again, don't box yourself in needlessly.
Yes its correct assumption, Hodgman. I just wan't to emulate the valarray class
but with custom functions for my needs. For those who don't know me yet, I use
C++.
but with custom functions for my needs. For those who don't know me yet, I use
C++.
Quote:Original post by Liam M
Actually, come to think of it, you can do it using template specialisation, like so:template <class T> class MyClass{public: MyClass();}; template <> MyClass<int>::MyClass(){ //Constructor junk.}int main(){ CTest<int> t; return 0;}
Way to much work for me.
Quote:Original post by tnutty
Way to much work for me.
Gotta love that can-do attitude.
Then don't specialise, see my first post.
I agree with Liam M that this is a bad idea - why do you need to restrict the set of template arguments?
However, here's another solution:
However, here's another solution:
template<class T> struct IsNumber { const static int value = 0; };template<> struct IsNumber<int> { const static int value = 1; };template<> struct IsNumber<unsigned int> { const static int value = 1; };template<> struct IsNumber<short> { const static int value = 1; };template<> struct IsNumber<unsigned short> { const static int value = 1; };template<> struct IsNumber<float> { const static int value = 1; };template<> struct IsNumber<double> { const static int value = 1; };//add other allowed number types here...template<class T>class MyNumber{public: MyNumber() { int assert_is_number[ IsNumber<T>::value ];(void)assert_is_number; } T number;};MyNumber<int> foo;//okMyNumber<char> bar;//compiler error
Quote:Original post by Hodgman
However, here's another solution:*** Source Snippet Removed ***
Or you could just is Boost.TypeTraits' is_arithmetic<>.
SFINAE allows you to specify that a template will only be matched for specific types. Using this and type traits, we can construct a templated class that will only be matched for arithmetic types. This is somewhat easier than specializing on each type you wish to support (long, int, double, float, etc).
Note that boost is pretty much a prerequisite for easily doing this. More reading can be found on boost::enable_if.
template <typename T, typename Enable = void>class MyClass{};template <typename T>class <T, typename boost::enable_if<boost::is_arithmetic<T> >::type>{ // stuff};
Note that boost is pretty much a prerequisite for easily doing this. More reading can be found on boost::enable_if.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement