交换并移动无限递归

问题描述 投票:1回答:2

我有一个非常简单的示例类,用C ++ 17编译。

#include <string>
#include <iostream>

struct name {
    std::string first, middle, last;
    name() = default;
    name(name const&) = default;
    name(name &&) = default;

    name& operator=(name o) {
        std::swap(*this, o); // bad!
        return *this;
    }
};
int main() {
    name n1{"mr", "john", "smith"};
    name n2 = std::move(n1); 
    name n3 = n2;

    std::cout << n3.first << " " << n3.middle << " " << n3.last;
} 

使用这个值语义,捆绑移动赋值和我故意称为合格交换,而不是using std::swap; swap(*this, o);。反正我没有提供我的交换。认为STL实现交换作为移动构造和一系列移动分配,我认为这个实现将无限递归,交换调用移动和移动调用交换。 std::swap是否改为成员交换或类似的东西?

c++ c++17 swap move-semantics
2个回答
5
投票

你从未援引过operator=;所有代码都使用initialization(调用构造函数),特别是copy initialization,而不是赋值(调用operator=)。

将代码更改为:

name n1{"mr", "john", "smith"};
name n2, n3;

n2 = std::move(n1); 
n3 = n2;

你会看到你使用的operator=(并且可能会爆炸)。


2
投票
  1. 鉴于你没有定义自己的swap(),编写std::swap(*this, o);using std::swap; swap(*this, o);是相同的事情(在代码示例的框架内)并且实际上没有强调这种写作的意图:在没有ADL的情况下,将使用std::swap版本在这两种情况下。
  2. 在你的声明中: name n2 = std::move(n1); name n3 = n2;

从不调用operator=(),初始化只能使用类构造函数完成。

© www.soinside.com 2019 - 2024. All rights reserved.