【发布时间】:2016-08-05 12:32:06
【问题描述】:
我尝试从临时对象中构建一对。据我了解, 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 的复制构造函数由 pair 调用。因为它已被删除,所以我收到错误 call to deleted constructor of 'Test'。但这似乎不是编译器的问题,因为我在 gcc 中遇到了类似的错误,请参阅https://ideone.com/n5GOeR。
有人可以向我解释一下为什么上面的代码无法编译吗?
【问题讨论】:
-
既然您无法复制或移动
Test,您希望std::pair的构造函数如何做到这一点? -
我尝试为
Test显式启用移动构造函数。 -
将移动构造函数放入
public部分后,it works just fine。