在this answer和this move-constructor specification之后,应该没有隐式的move构造函数或move赋值运算符。
但是,以下代码仍在gcc 7.2.1中编译:
#include <vector>
#include <iostream>
using namespace std;
struct NoCopyNoMove
{
NoCopyNoMove(const NoCopyNoMove&) = delete;
NoCopyNoMove& operator=(const NoCopyNoMove&) = delete;
NoCopyNoMove(NoCopyNoMove&&) = delete;
NoCopyNoMove& operator=(NoCopyNoMove&&) = delete;
NoCopyNoMove(int){};
};
struct NoCopy
{
NoCopy(const NoCopyNoMove&) = delete;
NoCopy& operator=(const NoCopyNoMove&) = delete;
// NoCopy(NoCopy&&) = delete;
// NoCopy& operator=(NoCopy&&) = delete;
~NoCopy() {
std::cout << "decontructor" << std::endl;
}
NoCopy(int){};
};
int main()
{
// vector<NoCopyNoMove> y; // fails!
vector<NoCopy> y;
y.emplace_back(1);
y.emplace_back(2);
}
答案 0 :(得分:5)
您的代码中有错字。您在哪里:
NoCopy(const NoCopyNoMove&) = delete;
NoCopy& operator=(const NoCopyNoMove&) = delete;
您应该拥有:
NoCopy(const NoCopy&) = delete;
NoCopy& operator=(const NoCopy&) = delete;
进行这些更改,代码确实无法编译: