免责声明:研究目标是如何为提供的代码部分禁用复制省略和返回值优化。如果要提及诸如XY问题之类的内容,请避免回答。问题具有严格的技术和研究性质,并以此方式强有力地提出
在C ++ 14中,引入了复制省略和返回值优化。如果某个对象已在一个表达式中被销毁并进行了复制构造,例如复制分配或从函数按值返回立即值,则将删除复制构造器。
以下推理应用于复制构造函数,但是可以对move构造函数执行类似的推理,因此不再赘述。
有一些针对自定义代码禁用复制省略的部分解决方案:
1) Compiler-dependent option. For GCC, there is solution based on __attribule__ or #pragma GCC constructions, like this /sf/answers/2343277541/ . But since it compiler-dependent, it does not met question.
2) Force-disabling copy-constructor, like Clazz(const Clazz&) = delete. Or declare copy-constructor as explicit to prevent it's using. Such solution does not met task since it changes copy-semantics and forces introducing custom-name functions like Class::copy(const Clazz&).
3) Using intermediate type, like describe here /sf/answers/1136663741/ . …