Search code examples
c++arrayscompiler-errorsstdarray

Compilation error when typedef an std::array


I define a type called inputTy using std::array (c++11) , the dimension of the array declared as an external constant integer d.

namespace project {
  namespace types{
    extern const int d;
    typedef std::array<double, d> inputTy;
  }
}

Why do I get such compilation error?

../../include/types.h:16:29: error: the value of ‘d’ is not usable in a constant expression
 typedef std::array<double, d> inputTy;
                             ^
../../include/types.h:15:18: note: ‘d’ was not initialized with a constant expression
 extern const int d;
              ^

Thanks for your help.


Solution

  • You can not use extern const int as array size because compiler does not know the size of a constant from other compilation unit.

    Change your design to use std::vector or some other container to overcome the problem or put the constant in a header and include it before typedefing.