仅在GCC和Clang中进行了测试,基类中存在传递值复制赋值运算符(在实现复制和交换(或复制和- move)习惯用法)导致隐式删除派生类中的副本分配运算符。
Clang和GCC对此表示同意; 为什么会这样?
示例代码:
#include <string>
#include <iostream>
struct base {
base() {
std::cout << "no-arg constructor\n";
}
base(const base& other) :
str{other.str} {
std::cout << "copy constructor\n";
}
base(base&& other) :
str{std::move(other.str)} {
std::cout << "move constructor\n";
}
base& operator=(base other) {
std::cout << "copy assigment\n";
str = std::move(other.str);
return *this;
}
base& operator=(base&& other) {
std::cout << "move assigment\n";
str = std::move(other.str);
return *this;
}
std::string str;
};
struct derived : base {
derived() = default;
derived(derived&&) = default;
derived(const derived&) = default;
derived& operator=(derived&&) = default;
derived& operator=(const derived&) = default;
};
derived foo() {
derived ret;
ret.str = "Hello,world!";
return ret;
}
int main(int argc,const char* const* argv) {
derived a;
a.str = "Wat";
a = foo(); // foo() returns a temporary - should call move constructor
return 0;
}