c – 如何使用一个unordered_map,其中值类型是它所在的类?

c – 如何使用一个unordered_map,其中值类型是它所在的类?,第1张

概述这段代码: class Foo { std::unordered_map<std::string, Foo> x;}; 给我一个错误: /usr/include/c++/4.7/bits/stl_pair.h:94:11: error: 'std::pair<_T1, _T2>::second' has incomplete typefoo.cpp:4:7: error: forw 这段代码:
class Foo {    std::unordered_map<std::string,Foo> x;};

给我一个错误:

/usr/include/c++/4.7/bits/stl_pair.h:94:11:  error: 'std::pair<_T1,_T2>::second' has incomplete typefoo.cpp:4:7: error: forward declaration of 'class Foo'

但是,这段代码编译的很好:

class Foo {    std::vector<Foo> x;};

这是一个库/编译器的BUG?

解决方法 C标准为各种智能指针指定允许模板参数为不完整类型.

此信息不是容器类型.因为它是未指定的,所以允许一个实现接受一个容器类模板的不完整类型而不是另一容器类模板,并且仍然是一致的.

为了使您的代码可移植,避免在类型完成之前制作任何类型的容器.

正式地,这个约束在以下适用于你的代码的规则(17.6.4.8)中找到:

In certain cases (replacement functions,handler functions,operations on types used to instantiate standard library template components),the C++ standard library depends on components supplIEd by a C++ program. If these components do not meet their requirements,the Standard places no requirements on the implementation.

In particular,the effects are undefined in the following cases:

if an incomplete type is used as a template argument when instantiating a template component,unless specifically allowed for that component.
总结

以上是内存溢出为你收集整理的c – 如何使用一个unordered_map,其中值类型是它所在的类?全部内容,希望文章能够帮你解决c – 如何使用一个unordered_map,其中值类型是它所在的类?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1253690.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存