I can not initialize LENGTH_STRING , the compiler swears at the last line " error C2131: выражение не определяется константой ", and if you do LENGTH_STRING=100 , then everything works. Is there a way to initialize LENGTH_STRING through previous constants?

 #include <climits> inline int getT(unsigned int x) { int p = 0; while (x > 10) { p++; x /= 10; } return p; } const unsigned int N = USHRT_MAX - 1; const int T = getT(N); const int LENGTH = 1000; const int LENGTH_STRING = (T+1)*LENGTH; 
  • VC ++ 2015 compiles without problems. - Harry
  • 2
    Specify the compiler and standard C ++. g ++ does not break off. - PinkTux
  • @PinkTux I use the Visual Studio 2015 Community with its standard out-of-the-box compiler. - user228430
  • In my cpp file it also works, but in the header it swears - user228430
  • Does constexp not help in this example? - pavel

1 answer 1

Define a function, for example, as follows.

 constexpr unsigned int getT( unsigned int n ) { const unsigned int Base = 10; return 1 + ( n / Base ? getT( n / Base ) : 0); }