std::std::unique_ptr不';不行? 请考虑以下C++ 20程序: #include <any> #include <memory> int main() { std::any a; std::unique_ptr<int> x = std::make_unique<int>(42); a.emplace<std::unique_ptr<int>>(std::move(x)); } #包括 #包括 int main(){ std::任何a; std::unique_ptr x=std::make_unique(42); a、 炮位(std::move(x)); }

std::std::unique_ptr不';不行? 请考虑以下C++ 20程序: #include <any> #include <memory> int main() { std::any a; std::unique_ptr<int> x = std::make_unique<int>(42); a.emplace<std::unique_ptr<int>>(std::move(x)); } #包括 #包括 int main(){ std::任何a; std::unique_ptr x=std::make_unique(42); a、 炮位(std::move(x)); },c++,c++20,C++,C++20,我得到: test.cc: In function ‘int main()’: test.cc:7:47: error: no matching function for call to ‘std::any::emplace<std::unique_ptr<int, std::default_delete<int> > >(std::remove_reference<std::unique_ptr<int>&>::type)’

我得到:

test.cc: In function ‘int main()’:
test.cc:7:47: error: no matching function for call to ‘std::any::emplace<std::unique_ptr<int, std::default_delete<int> > >(std::remove_reference<std::unique_ptr<int>&>::type)’
    7 |   a.emplace<std::unique_ptr<int>>(std::move(x));
      |                                               ^
In file included from test.cc:1:
/usr/include/c++/9/any:273:7: note: candidate: ‘template<class _ValueType, class ... _Args> typename std::any::__any_constructible<std::any::_Decay<_ValueType>&, std::any::_Decay<_ValueType>, _Args&& ...>::type std::any::emplace(_Args&& ...)’
  273 |       emplace(_Args&&... __args)
      |       ^~~~~~~
/usr/include/c++/9/any:273:7: note:   template argument deduction/substitution failed:
/usr/include/c++/9/any: In substitution of ‘template<class _ValueType, class ... _Args> typename std::any::__any_constructible<std::any::_Decay<_ValueType>&, std::any::_Decay<_ValueType>, _Args&& ...>::type std::any::emplace(_Args&& ...) [with _ValueType = std::unique_ptr<int>; _Args = {std::unique_ptr<int, std::default_delete<int> >}]’:
test.cc:7:47:   required from here
/usr/include/c++/9/any:273:7: error: no type named ‘type’ in ‘struct std::enable_if<false, std::unique_ptr<int>&>’
/usr/include/c++/9/any:288:7: note: candidate: ‘template<class _ValueType, class _Up, class ... _Args> typename std::any::__any_constructible<std::any::_Decay<_ValueType>&, std::any::_Decay<_ValueType>, std::initializer_list<_Up>, _Args&& ...>::type std::any::emplace(std::initializer_list<_Up>, _Args&& ...)’
  288 |       emplace(initializer_list<_Up> __il, _Args&&... __args)
      |       ^~~~~~~
/usr/include/c++/9/any:288:7: note:   template argument deduction/substitution failed:
test.cc:7:47: note:   ‘std::unique_ptr<int>’ is not derived from ‘std::initializer_list<_Up>’
    7 |   a.emplace<std::unique_ptr<int>>(std::move(x));
test.cc:在函数“int main()”中:
test.cc:7:47:错误:调用“std::any::emplace(std::remove_reference::type)”时没有匹配的函数
7 | a.安置(标准::移动(x));
|                                               ^
在test.cc:1中包含的文件中:
/usr/include/c++/9/any:273:7:注意:候选者:'template typename std::any::u any_constructible::type std::any::emplace(_Args&&…)
273 |安置(_Args&&…_Args)
|       ^~~~~~~
/usr/include/c++/9/any:273:7:注意:模板参数推断/替换失败:
/usr/include/c++/9/any:替换“模板typename std::any::u any_constructible::type std::any::emplace(_Args&&…[带_ValueType=std::unique_ptr;_Args={std::unique_ptr}”):
测试。cc:7:47:此处需要
/usr/include/c++/9/any:273:7:错误:“struct std::enable_if”中没有名为“type”的类型
/usr/include/c++/9/any:288:7:注意:候选者:'template typename std::any::uu any_constructible::type std::any::emplace(std::initializer_list,_Args&&…)
288定位(初始值设定项列表、参数和…)
|       ^~~~~~~
/usr/include/c++/9/any:288:7:注意:模板参数推断/替换失败:
test.cc:7:47:注意:“std::unique_ptr”不是从“std::initializer_list”派生的
7 | a.安置(标准::移动(x));

有什么想法吗?

这个类
std::Any
是可以复制的。尽管名称不同,但它不能包装“any”和所有类型。特别是,它不能包装不可复制的类型<代码>std::unique_ptr不可复制
std::unique\u ptr
不能存储在
std::any

哦,std::any要求包含的类型是可复制构造的吗?那么std::any不能保存move-only类型吗?是时候提议将
std::any
重命名为
std::几乎所有的
@CoryKramer了也许我们可以得到
std::move
->
std::实际上不把任何东西移动到同一个位置proposal@CoryKramer
std::some
:o)则是所包含对象的复制要求
std::any
可复制的后果是什么?类似于
std::function
@ローウ 我怀疑这是有关系的。我想可能已经可以指定将包装不可复制的复制
std::any
改为抛出,但这不是作者选择的。可能还有其他我没有想到的原因。