1

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.

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
zell
  • 9,830
  • 10
  • 62
  • 115
  • I think the value of `d` must be compile-time constant. When it is extern, how should the compiler know? – helb May 13 '15 at 13:39
  • possible duplicate of [What are the requirements for C++ template parameters?](http://stackoverflow.com/questions/643763/what-are-the-requirements-for-c-template-parameters) – martin May 13 '15 at 13:39

1 Answers1

3

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.

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100