user1364540
user1364540

Reputation: 33

Is there a way to use explicit instantiation to set member variables by type

Simplified example of what I am trying to do:

If I have a class:

template <typename T>
class Foo
{
public:
    Foo() : numericType(false){};

private:
    bool numericType;
}

And I want it to set awesomeType to true if T is an numeric type but false otherwise. Can I do this using explicit instantiation? Where each type I want to set numericType to true is explicitly instantiated and sets it to true, while all other types are not explicitly instantiated so remain false?

If not, how would you go about this?

Side Note: I cannot use C++11 thanks to my intel compiler.

Upvotes: 3

Views: 82

Answers (3)

Johan
Johan

Reputation: 3778

This is mostly what is done in std::is_arithmetic.

I've just seen that you're not allowed to use C++11... Try boost type traits is_arithmetic.

The standard "stole" this one from boost.

Upvotes: 1

Tim Seguine
Tim Seguine

Reputation: 2914

Since you can't use C++11, then you can follow the advice of the others but with Boost instead of STL:

http://www.boost.org/doc/libs/1_55_0/libs/type_traits/doc/html/index.html

There is not really any magic here though. Just template specialization. You would be able to implement it on your own even if it is cumbersome.

Upvotes: 2

Kerrek SB
Kerrek SB

Reputation: 477238

There is an existing type trait you can use to identify arithmetic types: From <type_traits>, the boolean std::is_arithmetic<T>::value indicates whether T is an arithmetic type.

Since this is a property of the type, and not of any particular object of this type, you should not make this boolean a non-static data member. At best, it should be a static, constant data member, but you might just as well not bother and use the trait directly when needed.

Upvotes: 4

Related Questions