dimanche 22 février 2015

Is there a difference in C++ between copy initialization and direct initialization? (C++11 version)

I just saw a similar question, but it's from 5 years ago.


Now we have C++11, introducing new moving semantics which may change how lines in the question above are interpreted. So, how would these lines behave in the new standard?



struct POD {
double doge;
}

class CopyCtible {
public:
CopyCtible();
CopyCtible( int );
CopyCtible( CopyCtible const& );
CopyCtible( CopyCtible&& ) = delete;
~CopyCtible();
}

class CopyCtibleEx {
public:
CopyCtibleEx();
CopyCtibleEx( int );
explicit CopyCtibleEx ( CopyCtibleEx const& );
CopyCtibleEx( CopyCtibleEx&& ) = delete;
~CopyCtibleEx();
}

class MoveCtible {
public:
MoveCtible();
MoveCtible( int );
MoveCtible( MoveCtible const& ) = delete;
MoveCtible( MoveCtible&& );
~MoveCtible();
}

class BothCtible {
public:
BothCtible();
BothCtible( int );
BothCtible( BothCtible const& );
BothCtible( BothCtible&& );
~BothCtible();
}

POD pod1 = POD();
POD pod2 = pod2;

CopyCtible cc1 = CopyCtible();
CopyCtible cc2 = 10;

CopyCtibleEx cce2 = 20;

MoveCtible mc1 = MoveCtible();
MoveCtible mc2 = 30;

BothCtible bc1 = BothCtible();
BothCtible bc2 = 42;


I intentionally removed less interesting lines (like MoveCtible mc3 = mc1; - oh come on, it's boringly obvious a good compiler will just tell you to lay your keyboard and walk away), but, if you less hardcore folks wish, I may add them too.


Aucun commentaire:

Enregistrer un commentaire