According to Compiler Explorer, only some will build this code:
struct s { static int i; };
int main( int argc, char *argv[] )
{
s::i = 1;
return 0;
}
Most newer C++ compilers fail at linking.
So, is this undefined behaviour or should this work?
This code violates the One Definition Rule (ODR), which requires a single definition of every entity that is used in the program.
There is no definition of s::i
in the program, but you are using it, and so the code violates the ODR. Any violation of the ODR makes the code ill-formed, no diagnostic required. This means the compiler can do anything it wants, including rejecting the code, or compiling it and producing an executable program (which could do anything it wants).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With