Let thing be a default-constructible and move-constructible type. Consider the following two initializations:
thing t1; // (1)
auto t2 = thing{}; // (2)
-
(1) default-constructs
t1. -
(2) default-constructs a temporary of type
thingand moves it intot2.
Under which circumstances will the move in (2) be elided?
In modern style C++ initializations, the type usually either doesn't appear at all or appears on the right. For example:
auto copy = vec; // Type doesn't appear
auto p_derived = std::make_unique<derived>(); // Type appears naturally on the right
// Putting the type on the right here makes it explicit that the upcast is intended
auto p_base = std::unique_ptr<base>{ std::make_unique<derived>() };
Using (2) would be consistent with those styles, whereas using (1) wouldn't.
Also, when using (1) with a long typename, the variable name appears far on the right. When using (2), the variable name is equally appparent no matter how long the typename is.
Does that mean that (2) is preferable over (1) for the sake of consistency?
Aucun commentaire:
Enregistrer un commentaire