【发布时间】:2021-05-02 09:18:31
【问题描述】:
将std::unique_ptr 放在std::tuple 中没有任何问题,但是当tuple 包含另一个tuple 和unique_ptr 作为元素时,编译器会抛出错误。
示例:
std::tuple<int, std::unique_ptr<Entity>> tupleA {1, std::move(new Entity)};
//this line throws an error!
std::tuple<std::tuple<int, int>, std::unique_ptr<Entity>> tupleB {{1, 1}, std::move(new Entity)};
第二行,创建 `tupleB` 抛出以下错误:
error: no matching constructor for initialization of ´std::tuple<std::tuple<int, int>,std::unique_ptr<Entity>>´
note: candidate constructor template not viable: cannot convert initializer list argument to ´std::allocator_arg_t´
这到底是什么问题?
【问题讨论】:
-
在构造函数中尝试
std::tuple<int, int>{1, 1} -
应该是:
auto tupleB = std::make_tuple(std::make_tuple(1, 1), std::make_unique<Entity>());
标签: c++ pointers stl unique-ptr stdtuple