c++ - non-generated special member functions vs deleted special member functions -


this question has answer here:

this compiles , calls copy constructor:

struct foo {     foo() = default;     foo(const foo&) { cout << "copy ctor!" << endl; }     //foo(const foo&&) = delete; };  int main() {     foo a;     foo b(move(a)); 

this not compile:

struct foo {     foo() = default;     foo(const foo&) { cout << "copy ctor!" << endl; }     foo(const foo&&) = delete; };  int main() {     foo a;     foo b(move(a)); 

i know in first case why copy called - move ctor not generated. why doesn't second snipper compile? thought call copy ctor again.

here link online compiler

the difference boils down absence of move constructor versus deleted move constructor. 2 not equivalent.


in first case, presence of copy constructor prevents generation of implicit move constructor. hence overload resolution on foo(foo&&) find 1 single viable candidate:

foo(const foo& ); 

that candidate selected default.

in second case, have move constructor. overload resolution find 2 viable candidates:

foo(const foo& ); // before foo(foo&& );      // 1 exists 

the move constructor better match, it's picked best viable candidate. however, since it's explicitly defined deleted, selection of ill-formed. hence compile error.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -