Initializing a member variable with a pointer to itself

I heard that the following is true and leaves xuninitialized, as if it were int x;:

int x = x;

How about this one? Is this code equivalent above:

struct Foo
{
    Foo(int Foo::*p) : x(this->*p) { }
    int x;
};

int main() {
    Foo f(&Foo::x);
}

Is it f.xstill uninitialized? Do I have undefined behavior?

+4
source share
2 answers

C ++ 14 makes it clear that using the undefined value undefined from a section 8.5(highlighting mine):

, . , , , , (5.17 [expr.ass]). [: , . 3.6.2 [basic.start.init]. - note] , undefined, :

char. , 3.3.2 :

int x = 12;
{ int x = x; }

To:

unsigned char x = 12;
{ unsigned char x = x; }

, .

x , , , undefined.

+2

:

, , : x(x), x , x .

+1

All Articles