vendredi 1 juillet 2016

Construction in return statement

Suppose we have a class Foo with a non-explicit constructor from an int. Then for the following functions:

Foo makeFoo1() { return 123; }
Foo makeFoo2() { return {123}; }

I think makeFoo1 requires that Foo's copy/move ctor is accessible, and it's possible (though unlikely) that the compiler does not elide the copy and thus results in a true copy/move.

For makeFoo2, since we are using copy-list-initialization, no copy/move can ever occurred.

Should I really worry about this and put arguments to non-explicit ctors in braces whenever I can (as in makeFoo2)?

Aucun commentaire:

Enregistrer un commentaire