0

我正在做一些实验,看看除了复制省略、RVO、NRVO 案例之外何时执行复制。

所以我写了一些这样的代码:

class X {
 public:
  X() { std::cout << "Default constructor" << std::endl; }

  X(const X&) { std::cout << "Copy constructor" << std::endl; }

  X(X&&) { std::cout << "Move constructor" << std::endl; }

  X& operator=(const X) {
    std::cout << "Assignment operator" << std::endl;
    return *this;
  }

  X& operator=(X&&) {
    std::cout << "Move assignment operator" << std::endl;
    return *this;
  }

  ~X() { std::cout << "Destructor" << std::endl; }
};

class Y {
 private:
  X x;

 public:
  const X& getX() const {
    std::cout << "getX" << std::endl;
    return x;
  }
};

int main() {
  Y y;
  std::cout << "assign to ref" << std::endl;
  const X& x1 = y.getX();
  (void)x1;
  std::cout << "assign to const" << std::endl;
  const X x2 = y.getX();
  return 0;
}

我收到以下输出:

Default constructor
assign to ref
getX
assign to const
getX
Copy constructor
Destructor
Destructor

使用 gcc 编译或使用 -O3 编译并尝试 -std=c++{11,14,17} 都产生相同的输出。

令我惊讶的是,我没想到使用 y.getX(); 时会执行任何复制;到一个 const 变量。这是我经常使用的东西,只是为了简化我在以下代码中对该变量及其成员的访问,但我并没有通过 const 引用执行它,而是我只是使用 const 希望编译器将它视为重命名。

有谁知道为什么要执行该副本?我想到的唯一原因是它使代码线程安全。如果有多个线程处理对象 y,那么我对 const 的分配毕竟不会是那个 const。因为它只会引用对象 y 中的成员 x。这可能会被其他线程更改。但我不确定这是否是真正的意图。

4

1 回答 1

0

要查看 RVO 与编译器强制使用 NRVO 的效果,请-fno-elide-constructors在以下修改后的程序上使用编译器开关。使用通常的选项,您可以获得:

Default constructor 1
assign to ref
getX (with id: 1)
x1 (id:1)
assign to const
getX (with id: 1)
Copy constructor 2
x2 (id:2)
make_X copy
Default constructor 3
make_X (with id: 3)
x3 (id:3)
make_X ref
Default constructor 4
make_X (with id: 4)
x4 (id:4)
Destructor 4
Destructor 3
Destructor 2
Destructor 1

但是使用 NRVO,您可以获得:

Default constructor 1
assign to ref
getX (with id: 1)
x1 (id:1)
assign to const
getX (with id: 1)
Copy constructor 2
x2 (id:2)
additional 1
Default constructor 3
make_X (with id: 3)
Move constructor 4
Destructor 3
Move constructor 5
Destructor 4
x3 (id:5)
additional 2
Default constructor 6
make_X (with id: 6)
Move constructor 7
Destructor 6
x4 (id:7)
Destructor 7
Destructor 5
Destructor 2
Destructor 1

代码示例:

#include <iostream>
int global_id;
class X {
public:
    X() : id(++global_id) {
        std::cout << "Default constructor " << id << std::endl;
    }
    X(const X&) : id(++global_id) {
        std::cout << "Copy constructor " << id << std::endl;
    }
    X(X&&) : id(++global_id) {
        std::cout << "Move constructor " << id << std::endl;
    }
    X& operator=(const X&) {
        std::cout << "Assignment operator " << id << std::endl;
        return *this;
    }
    X& operator=(X&&) {
        std::cout << "Move assignment operator " << id << std::endl;
        return *this;
    }
    ~X() {
        std::cout << "Destructor " << id << std::endl;
    }
    int id;
};

class Y {
    X x;
public:
    const X& getX() const {
        std::cout << "getX (with id: " << x.id << ')' << std::endl;
        return x;
    }
    X make_X() const {
        X extra;
        std::cout << "make_X (with id: " << extra.id << ')' << std::endl;
        return extra;
    }
};

int main()
{
    Y y;
    std::cout << "assign to ref" << std::endl;
    const X& x1 = y.getX();
    std::cout << "x1 (id:" << x1.id << ")\n";
    (void) x1;
    std::cout << "assign to const" << std::endl;
    const X x2 = y.getX();
    std::cout << "x2 (id:" << x2.id << ")\n";
    std::cout << "make_X copy" << std::endl;
    const X x3 = y.make_X();
    std::cout << "x3 (id:" << x3.id << ")\n";
    std::cout << "make_X ref" << std::endl;
    const X& x4 = y.make_X();
    std::cout << "x4 (id:" << x4.id << ")\n";
    return 0;
}

如您所见,RVO 实际上只与局部变量一起使用。

于 2017-12-25T18:29:46.990 回答