Why can't I use brace enclosed initializer list to call copy constructor?

c++11

Solution

This is a known problem with the C++11 wording. The C++14 CD doesn't have this problem fixed either (see http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1467 for the corresponding DR).

We can hope for compilers to implement a future fix for this in their C++11 and C++14 mode retroactively. The next issues list revision should contain proposed wording for issue 1467 that fixes this issue.

Problem

I was trying to run the code from the new C++ Programming Language book written by Bjarne Stroustrup seems like does not work. Which compiler supports the the grammar `S y {x};` in the code? Can not compile, I tried g++, vc++, not yet Clang, that error code supposes to be an initialization, after, I changed that code to `S y = x;` an assignment, but did not output the result as comments , am I wrong somewhere? ``` struct S {      int* p;    // a pointer }; S x {new int{0}}; void f() {      S y {x};              // "copy" x      *y.p = 1;             // change y; affects x      *x.p = 2;             // change x; affects y      delete y.p;           // affects x and y      y.p = new int{3};     // OK: change y; does not affect x      *x.p = 4;             // oops: write to deallocated memory } ``` Then I rewrote the code in C++03 version, It work as described, like this: ``` struct S { int *p; }; int main() { S x; x.p = new int; *(x.p) = 0; S y = x; *y.p = 1; *x.p = 2; delete y.p; y.p = NULL; x.p = NULL; y.p = new int; *(y.p) = 3; *(x.p)= 4; } ``` Is there any magic behind, or just the code in the book is not correct. Any advise thanks.

Original source