c++ - 在 C++ 中禁用复制省略

标签 c++ copy-constructor

免责声明:研究的目标是如何为提供的代码部分禁用复制省略和返回值优化。如果想提及 XY 问题之类的问题,请避免回答。该问题具有严格的技术和研究性质,并以这种方式强烈提出

在 C++14 中引入了复制省略和返回值优化。如果某个对象已在一个表达式中被析构和复制构造,例如复制赋值或按值从函数返回立即值,则复制构造函数将被省略。

以下推理适用于复制构造函数,但移动构造函数可以进行类似推理,因此不再进一步考虑。

有一些针对自定义代码禁用复制省略的部分解决方案:

1) 依赖于编译器的选项。对于 GCC,有基于 __attribule__#pragma GCC 构造的解决方案,例如 https://stackoverflow.com/a/33475393/7878274 .但由于它依赖于编译器,因此没有遇到问题。

2) 强制禁用复制构造函数,如 Clazz(const Clazz&) = delete。或者将复制构造函数声明为 explicit 以防止它被使用。这种解决方案不符合任务,因为它改变了复制语义并强制引入自定义名称函数,如 Class::copy(const Clazz&)

3) 使用中间类型,就像这里描述的那样https://stackoverflow.com/a/16238053/7878274 .由于此解决方案强制引入新的后代类型,因此未遇到问题。

经过一些研究发现恢复临时值可以解决问题。如果将源类重新解释为对具有此类的单元素数组的引用并提取第一个元素,则复制省略将关闭。模板函数可以这样写:

template<typename T, typename ... Args> T noelide(Args ... args) {
    return (((T(&)[1])(T(args...)))[0]);
}

这种解决方案在大多数情况下效果很好。在下面的代码中,它生成三个复制构造函数调用——一个用于直接复制赋值,两个用于从函数返回的赋值。它在 MSVC 2017 中运行良好

#include <iostream>

class Clazz {
public: int q;
    Clazz(int q) : q(q) { std::cout << "Default constructor " << q << std::endl; }
    Clazz(const Clazz& cl) : q(cl.q) { std::cout << "Copy constructor " << q << std::endl; }
    ~Clazz() { std::cout << "Destructor " << q << std::endl; }
};

template<typename T, typename ... Args> T noelide(Args ... args) {
    return (((T(&)[1])(T(args...)))[0]);
}

Clazz func(int q) {
    return noelide<Clazz>(q);
}

int main() {
    Clazz a = noelide<Clazz>(10);
    Clazz b = func(20);
    const Clazz& c = func(30);
    return 0;
}

此方法适用于 ab 情况,但对情况 c 执行冗余复制 - 而不是复制,对临时的引用应该与生命周期扩展一起返回。

问题:如何修改noelide 模板以允许它与具有生命周期扩展的const 左值引用一起正常工作? 谢谢!

最佳答案

根据 N4140,12.8.31:

...

This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):

(31.1) — in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter) with the same cv-unqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value

(31.3) — when a temporary class object that has not been bound to a reference (12.2) would be copied/moved to a class object with the same cv-unqualified type, the copy/move operation can be omitted by constructing the temporary object directly into the target of the omitted copy/move

因此,如果我理解正确,复制省略只会发生,如果返回语句是局部变量的名称。因此,您可以通过返回例如“禁用”复制省略。 return std::move(value) ... 如果你不喜欢使用 move为此,您可以简单地实现 noelide作为static_cast<T&&>(...) .

关于c++ - 在 C++ 中禁用复制省略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55490431/

相关文章:

c++ - id3lib 链接器错误

matlab - 复制子类对象的父类(super class)部分

c++ - 为什么下面的代码也会调用拷贝构造函数呢?

c++ - 使用tinyxml创建xmlns

c++ - 在 C++ 中将 int 设置为 Infinity

c++ - Visual Studio 2010 的 dword ptr [...] 问题

c++ - 包含动态分配数组的结构中的复制构造函数是否总是必要的?

c++ - 为什么用户定义的移动构造函数会禁用隐式复制构造函数?

c++ - 实现提供的复制构造函数和赋值运算符

c++ - 我们可以使用继承来实现链表吗?