I believe the code doesn’t compile, because I’m using the extern const int j to initialize i in class A. But according to the Standard, why is this wrong ?
File A.h
extern const int j;
class A
{
static const int i = j; // error C2057:expected constant expression
};
File A.cpp
#include "A.h"
const int j = 10;
int main()
{
A a;
}
jis a constant variable, not a compile-time constant.The compiler can’t know the value of
jin the translation unit that compilesclass A. For example, the following would work:Note that in this case
jwill not be global, but a copy will exist for each TU. The snippet is here just to prove a point.