i was doing some tests of the move semantics and I tried this :
class A{
public:
A(){printf("A CTOR\n");}
A(A&) {printf("A CTOR by copy\n");}
A(A&&){printf("A CTOR by universal reverence\n");}
};
A&& create(){
A v;
return std::move(v);
}
auto x{ create() };//this does not compile
float d[]{1.2,1.3,5,6};//this does compile
I get the following error:
error C3086: cannot find 'std::initializer_list': you need to #include <initializer_list>
I don’t understand as the initializer list feature has been added to VC11 with CTP2012 nov.
Is this because we have to wait for an update of the stdlib ?
I think the code is correct as I copied it from a slide from Scott meyers: Move Semantics, Rvalue References, Perfect Forwarding.
Thank you for your help.
For your information, the spurious copies occured because i did not add “const” in my CTOR by copy.
Best
Braces with
autowill always end up instd::initializer_list<T>type. So basically what happens here you try to create astd::initializer_list<A>here forxinstead ofAwhat probably was your intent here. However your code should compile fine and that may be a bug in the VS latest CTP compiler.To make
xbe ofAyou have 2 options:Do not use
autohere:Do not use Uniform initialization here:
Beside that I see 2 other issues in you code. Firstly signature of proper copy-constructor should look like that:
Moreover it is not encouraged to return RValue references from functions. Instead you should return by value like that:
or just:
EDIT:
Oh and just to be politically correct
A(A&&)is not “A CTOR by universal reference”. It is “A CTOR by move” or “A CTOR by rvalue reference” depending on which level of abstraction you want to operate. Universal reference is always about templates and that specific type type deduction.Please also read an article of Scott Meyers I attached in a different comment http://scottmeyers.blogspot.com/2012/10/copying-constructors-in-c11.html where the same C++ guru that you refer in your notes exactly explains the problems you face. Hope that helps.