A complete-class context of a class is a
(6.1) function body,
(6.2) default argument,
(6.3) noexcept-specifier ([except.spec]),
(6.4) contract condition, or
(6.5) default member initializerwithin the member-specification of the class. [ Note: A complete-class context of a nested class is also a complete-class context of any enclosing class, if the nested class is defined within the member-specification of the enclosing class. — end note ]
The highlighted text above seems to give support to the following snippet:
#include<iostream>
struct A{
int i = j + 1;
int j = 1;
};
int main(){
A a;
std::cout << a.i << '\n';
std::cout << a.j << '\n';
}
, and I was expecting it to print
2
1
Both GCC and clang print
1
1
but in addition clang gives the following warning:
prog.cc:3:13: warning: field 'j' is uninitialized when used here [-Wuninitialized]
int i = j + 1;
^
prog.cc:8:7: note: in implicit default constructor for 'A' first required here
A a;
^
prog.cc:2:8: note: during field initialization in the implicit default constructor
struct A{
^
1 warning generated.
My assumption is that the code is ill-formed NDR. But why?