Restrict Template Parameter to an Integer Type

So, I want to write a class that's specialized depending on integer type (signed / unsigned and 1 / 2 / 4 / 8 bytes). I can use a template class:

template<typename T>
class Test {
  private:
    T internalVariable;
};

But, is there anyway to restrict this to just the integer types? So, it would cause a compiler error if someone tried to instantiate the class with a non-integer such as:

Test<float> x;

Also, I'd like this to work on an AVR-based board. So, no functions from the STL.

Thanks.

Use a static_assert with std::is_integral.

If you don't have access to the standard library, you'll have to roll your own and put it somewhere in a compatibility header:

struct false_type { static constexpr bool value = false; };
struct true_type { static constexpr bool value = true; };

template <class T> struct my_is_integral : false_type {};
template <> struct my_is_integral<short> : true_type {};
template <> struct my_is_integral<unsigned short> : true_type {};
template <> struct my_is_integral<int> : true_type {};
template <> struct my_is_integral<unsigned int> : true_type {};
template <> struct my_is_integral<long> : true_type {};
template <> struct my_is_integral<unsigned long> : true_type {};
template <> struct my_is_integral<long long> : true_type {};
template <> struct my_is_integral<unsigned long long> : true_type {};
// Note: not the same as std::is_integral, because this version ignores
// character types, bool, and cv-qualified versions.

template <typename T>
class Test {
  private:
    static_assert(my_is_integral<T>::value, "T must be an integral type");
    T internalVariable;
};

Test<float> x;
In instantiation of 'class Test<float>':
    error: static assertion failed: T must be an integral type

Thanks!

Additional question about these struct definitions:

Not familiar with that syntax. They seem to be creating member variables (using what look like "initial lists") of type true_type or false_type ? But there are no member variables specified in the body of the struct? And there are no constructors, which is where one normally finds initializer lists? And those "member variables" are accessed without names? The scope specifier goes straight to the "value" member?

static_assert(my_is_integral<T>::value, "T must be an integral type");

It's just inheritance, they so they inherit the static value member from either the true_type or false_type struct.

Doooh ...