/// Bug Report struct test { int x = data; int data; test(int o) : data(o) { } }; /// Potentially Related Issue struct test2 { int x; int data; test2(int o) : data(o), x(data) { } }; /* - Details - There should be a warning/error for test and test2. In test the default initialization of x relies on data, which is initialized after x. In test2 the constructor initialization list is out of order. Same result as for test. - Note - clang displays a warning for both these cases, but neither gcc nor msvc do */
Did you forget to enable warnings? GCC warns about test2 but not test: $ g++ -Wall -c 85691.cc 85691.cc: In constructor ‘test2::test2(int)’: 85691.cc:14:13: warning: ‘test2::data’ will be initialized after [-Wreorder] int data; ^~~~ 85691.cc:13:13: warning: ‘int test2::x’ [-Wreorder] int x; ^ 85691.cc:16:9: warning: when initialized here [-Wreorder] test2(int o) : data(o), x(data) { } ^~~~~ Clang has the same behaviour for -Wreorder, but it additionally warns about test::data with -Wuninitialized: 85691.cc:4:17: warning: field 'data' is uninitialized when used here [-Wuninitialized] int x = data; ^ 85691.cc:7:9: note: during field initialization in this constructor test(int o) : data(o) { } ^ 85691.cc:16:24: warning: field 'data' will be initialized after field 'x' [-Wreorder] test2(int o) : data(o), x(data) { } ^ 85691.cc:16:35: warning: field 'data' is uninitialized when used here [-Wuninitialized] test2(int o) : data(o), x(data) { } ^ 3 warnings generated. We already have Bug 19808 covering this. *** This bug has been marked as a duplicate of bug 19808 ***