Communicator communicator = Communicator();
Communicator communicator;
这两个呼叫之间有什么区别?
区别在于复制省略。在C ++ 17之前,
Communicator communicator = Communicator();
Communicator
创建了一个临时对象,然后将其用于copy-construct communicator
。编译器可以对此进行优化,但是它必须检查是否可以调用copy或move构造函数(public,not delete,not explicit
)。
从C ++ 17开始,复制省略规则发生了变化:引入了“未实现的值传递”。现在,在该行中没有创建临时对象,也不需要复制/移动构造函数。
以下简单代码将在C ++ 17中编译,但在C ++ 11/14中将无法编译:
class Communicator {
public:
Communicator() = default;
Communicator(const Communicator&) = delete;
};
Communicator c = Communicator();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句