设thing
为默认构造和移动构造类型。考虑以下两个初始化:默认初始化与默认构建值的初始化初始化
thing t1; // (1)
auto t2 = thing{}; // (2)
(1)默认构造
t1
。(2)默认构建
thing
类型的临时表并将其移至t2
。
在何种情况下,(2)中的移动将被消除?
在现代风格的C++初始化中,类型通常根本不出现或出现在右侧。例如:
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>() };
使用(2)将与这些样式一致,而使用(1)则不会。
另外,当使用(1)long typename时,变量名称显示在右侧。当使用(2)时,无论类型名称的长度如何,变量名称同样适用。
这是否意味着(2)为了一致性而优于(1)?
没有关系,反正复制elsiion。 –