存储不完整类型的哪些STL数据结构可以用作类成员?
据我所知,由于C ++ 17,某些STL数据结构可能“存在”,而不完整的类型是描述存储的类型的模板参数。例如,我可能会使用 std :: simolor_ptr< code>
(我不确定它是否是数据结构)或 std :: vector< nistete>
作为类成员如果类的所有属性(需要不完整
的定义)在单独的.cpp文件中实现:
class Incomplete;
using Complete = int;
class Foo {
private:
std::unique_ptr<Incomplete> u_p;
std::vector<Incomplete> v;
std::deque<Incomplete> d;
std::list<Incomplete> l;
std::set<Incomplete> s;
std::unordered_map<Complete, Complete> u_m_cc;
std::unordered_map<Complete, Incomplete> u_m_ci;
std::unordered_map<Incomplete, Complete> u_m_ic;
std::unordered_map<Incomplete, Incomplete> u_m_ii;
public:
// implemented in a separate .cpp which has Incomplete defined:
Foo();
Foo(Foo&&);
Foo& operator=(Foo&&);
Foo(Foo const&);
Foo& operator=(Foo const&);
~Foo();
};
那么,上面列出的哪些数据成员适用于此类用法?其他数据结构,智能指针等呢?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设所有类都没有明确或隐式使用,直到类型完成为止:
std :: simory_ptr
和std :: sharde_ptr
始终可能是不完整的。 ++ 11,请参见 [simel.ptr]/5 和 [util.smartptr.shared]/2 。添加了至C ++ 17,但仅适用于
且仅当使用的分配器满足分配器完整性要求,即,即使值类型本身还不完整,分配器类型
x
本身是一种完整的类型,std :: allocator_traits&lt; x&gt;
的所有成员也是如此,除:: value_type
外。默认分配器std ::分配器
符合这些要求。其他容器都不能与不完整的类型一起使用。根据上面链接的提案,该范围仅限于这三个容器“ 作为第一步”,因为主要实施已经对其进行了支持。
Assuming none of the classes members are used explicitly or implicitly until the type is complete:
The template argument can always be incomplete for
std::unique_ptr
andstd::shared_ptr
since C++11, see [unique.ptr]/5 and [util.smartptr.shared]/2 respectively.Support of incomplete types in containers was added with N4510 to C++17, but only for
and only if the allocator used fulfills the allocator completeness requirements, namely that, even if the value type itself is not complete, the allocator type
X
itself is a complete type and so are all members ofstd::allocator_traits<X>
, except::value_type
. The default allocatorstd::allocator
fulfills these requirements.None of the other containers can be used with incomplete types. According to the proposal linked above the scope was limited to these three containers "as a first step" because the major implementations already had support for it.
这是不正确的。
由于C ++ 17,某些STL类型可能会以不完整的类型为模板参数声明。
到类型是实例化时,类型必须完成。
例如:(未经测试的代码)
This is incorrect.
Since C++17, some STL types may be declared with an incomplete type as the template parameter.
By the time the types are instantiated, the types must be complete.
For example: (untested code)