据我所知,由于C ++ 17,某些STL数据结构可能以不完整的类型作为模板参数“存在”,以描述存储的类型。例如,如果类的所有属性(需要std::unique_ptr<Incomplete>
的定义)都在单独的实现中,则我可以使用std::vector<Incomplete>
(虽然不确定它是否是数据结构)或Incomplete
作为类成员.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();
};
那么,上面列出的哪些数据成员对此用途有效?其他数据结构,智能指针等呢?
由于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