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.

2
  • I think the value of d must be compile-time constant. When it is extern, how should the compiler know? Commented May 13, 2015 at 13:39
  • possible duplicate of What are the requirements for C++ template parameters? Commented May 13, 2015 at 13:39

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.