Jump to content
  • Advertisement
Sign in to follow this  
tnutty

class template question

This topic is 3255 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
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]

Share this post


Link to post
Share on other sites
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?
template<class T>
class MyNumber
{
T number;
};
Is this a correct assumption?

Share this post


Link to post
Share on other sites
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; //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.

Share this post


Link to post
Share on other sites
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++.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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:
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;//ok
MyNumber<char> bar;//compiler error

Share this post


Link to post
Share on other sites
Quote:
Original post by Hodgman
However, here's another solution:*** Source Snippet Removed ***

Or you could just is Boost.TypeTraits' is_arithmetic<>.

Share this post


Link to post
Share on other sites
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).


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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!