20

I am looking to define a template class whose template parameter will always be an integer type. The class will contain two members, one of type T, and the other as the unsigned variant of type T -- i.e. if T == int, then T_Unsigned == unsigned int. My first instinct was to do this:

template <typename T> class Range {
    typedef unsigned T T_Unsigned; // does not compile
public:
    Range(T min, T_Unsigned range);
private:
    T m_min;
    T_Unsigned m_range;
};

But it doesn't work. I then thought about using partial template specialization, like so:

template <typename T> struct UnsignedType {}; // deliberately empty
template <> struct UnsignedType<int> {
    typedef unsigned int Type;
};

template <typename T> class Range {
    typedef UnsignedType<T>::Type T_Unsigned;
    /* ... */
};

This works, so long as you partially specialize UnsignedType for every integer type. It's a little bit of additional copy-paste work (slash judicious use of macros), but serviceable.

However, I'm now curious - is there another way of determining the signed-ness of an integer type, and/or using the unsigned variant of a type, without having to manually define a Traits class per-type? Or is this the only way to do it?

Blair Holloway
  • 15,969
  • 2
  • 29
  • 28

2 Answers2

24

The answer is in <type_traits>

For determining the signed-ness of a type use std::is_signed and std::is_unsigned.

For adding/removing signed-ness, there is std::make_signed and std::make_unsigned.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Terry Mahaffey
  • 11,775
  • 1
  • 35
  • 44
3

If you can't or don't want to depend on TR1/C++0x features, Boost.TypeTraits also offers you make_unsigned<> et al.

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
  • +1 - as it happens I'm using Visual Studio 2005 which doesn't support `` - so having an alternative is handy. I still accepted `` as the answer as it's part of the Standard Library. – Blair Holloway May 13 '10 at 05:10
  • @Blair: Careful, thats actually *"will be part of"* - its part of the next standard that is not out yet. – Georg Fritzsche May 13 '10 at 05:13