从临时对象创建一对
Creating a pair from temporaries
我试着用临时物构造一对。据我了解,std::pair 提供了必要的构造函数,但我无法使其工作。这是我的最小示例:
#include <utility>
struct Test {
Test() : a(1.0) {}
private:
double a;
Test(Test&&) = default;
Test(const Test&) = delete;
Test& operator=(Test&&) = delete;
};
int main (int argc, char** argv) {
std::pair<Test, double> result(Test(), 0.0);
}
我试图用 clang++-3.8 --std=c++14
编译它。
Test 的复制构造函数是成对调用的。因为它被删除了,所以我得到错误call to deleted constructor of 'Test'
。不过这似乎不是编译器的问题,因为我在 gcc 中遇到了类似的错误,请参阅 https://ideone.com/n5GOeR.
谁能给我解释一下为什么上面的代码编译失败?
我的 gcc (6.1.1) 给出的错误信息略有不同,更有帮助:
t.C:8:3: note: declared private here
Test(Test&&) = default;
^~~~
您的移动构造函数是私有的。显然必须是 public.
我试着用临时物构造一对。据我了解,std::pair 提供了必要的构造函数,但我无法使其工作。这是我的最小示例:
#include <utility>
struct Test {
Test() : a(1.0) {}
private:
double a;
Test(Test&&) = default;
Test(const Test&) = delete;
Test& operator=(Test&&) = delete;
};
int main (int argc, char** argv) {
std::pair<Test, double> result(Test(), 0.0);
}
我试图用 clang++-3.8 --std=c++14
编译它。
Test 的复制构造函数是成对调用的。因为它被删除了,所以我得到错误call to deleted constructor of 'Test'
。不过这似乎不是编译器的问题,因为我在 gcc 中遇到了类似的错误,请参阅 https://ideone.com/n5GOeR.
谁能给我解释一下为什么上面的代码编译失败?
我的 gcc (6.1.1) 给出的错误信息略有不同,更有帮助:
t.C:8:3: note: declared private here
Test(Test&&) = default;
^~~~
您的移动构造函数是私有的。显然必须是 public.