存储不完整类型的哪些STL数据结构可以用作类成员?

发布于 2025-01-28 14:32:27 字数 974 浏览 4 评论 0 原文

据我所知,由于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();
};

那么,上面列出的哪些数据成员适用于此类用法?其他数据结构,智能指针等呢?

As far as I know, since C++17 some STL data structures may "exist" with an incomplete type as the template parameter which describes the type stored. For example, I may use std::unique_ptr<Incomplete> (I'm not sure if it's a data structure though) or std::vector<Incomplete> as class members if all properties of the class (which need Incomplete's definition) are implemented in a separate .cpp file:

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();
};

So, which of the data members listed above are valid for such usage? What about other data structures, smart pointers etc.?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

微暖i 2025-02-04 14:32:27

假设所有类都没有明确或隐式使用,直到类型完成为止:

std :: simory_ptr std :: sharde_ptr 始终可能是不完整的。 ++ 11,请参见 [simel.ptr]/5 [util.smartptr.shared]/2

添加了至C ++ 17,但仅适用于

std::vector
std::list
std::forward_list

且仅当使用的分配器满足分配器完整性要求,即,即使值类型本身还不完整,分配器类型 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 and std::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

std::vector
std::list
std::forward_list

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 of std::allocator_traits<X>, except ::value_type. The default allocator std::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.

时光清浅 2025-02-04 14:32:27

由于C ++ 17,某些STL数据结构可能“存在”,而不完整的类型作为模板参数描述了存储的类型。

这是不正确的。

由于C ++ 17,某些STL类型可能会以不完整的类型为模板参数声明

到类型是实例化时,类型必须完成。

例如:(未经测试的代码)

struct T; // incomplete
using TV = std::vector<T>; // declared a type using incomplete type T; fine.

TV tv0; // attempt to declare a variable of type TV; fails to compile.

struct T { int v; }; // T is now complete
TV tv1; // compiles

since C++17 some STL data structures may "exist" with an incomplete type as the template parameter which describes the type stored.

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)

struct T; // incomplete
using TV = std::vector<T>; // declared a type using incomplete type T; fine.

TV tv0; // attempt to declare a variable of type TV; fails to compile.

struct T { int v; }; // T is now complete
TV tv1; // compiles
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文