I would like to know how to initialize an array in a class whose values can be used in constant-expressions.
Here is an explanation of my problem :
// The goal : initializing an array for a class
// whose values can be used as normal static const
// (as template parameters for example)
class MyClass
{
public:
static const unsigned int value = 42; // <- No problem here
static const unsigned int array[3] = {100, 101, 102}; // <- How to initialize this static const array (with constexpr or metaprogrammation maybe ?)
template<unsigned int T> inline void f() {std::cout<<"Hello, my value is "<<T<<std::endl;} // <- Simple function for demonstration purposes
inline void fvalue() {f<value>();} // <- No problem here
inline void farray() {f<array[1]>();} // <- Big problem here
};
//const unsigned int Class1::array[3] = {100, 101, 102}; // <- If I do this, I will initialize the array but farray() will not compile
Is there any way to do this in C++ 2011 ? (with constexpr or metaprogrammation maybe ?)
Thank you very much !
EDIT : As the title specify it, I need the array to be a member of the class (not a global array).
Yes, you can make it
constexpr..Making it
constexprallows the static member to have more types than just integral or enumeration types, when it is initialized in-class. In particular, the member just needs to be of a literal type, and all expressions in the initializer must be constant expressions. So this is fine