I have a template:
template<unsigned int N> struct IntN;
template <> struct IntN< 8> {
typedef uint8_t type;
};
template <> struct IntN<16> {
typedef uint16_t type;
};
And in main I initialise and alternate by doing this:
IntN< 8>::type c;
This seems to work, however, when I store the value inside a variable, it does not and I get the following error:
error: non-type template argument of type ‘int’ is not an integral constant expression
Here is an example of the code:
template<unsigned int N> struct IntN;
template <> struct IntN< 8> {
typedef uint8_t type;
};
template <> struct IntN<16> {
typedef uint16_t type;
};
int main(int argc, char *argv[]) {
int foo = 8;
IntN<foo>::type c;
}
Does anyone have any ideas? Thanks
The template arguments for integral template parameters must be constant expressions. An integral literal is a constant expression
A constant variable initialized with a constant expression is a constant expression
Here n is OK because it’s both const and initialized by a constant expression (8). The following will not compile though: