How to have an unordered_map where the value type is the class it's in?

后端 未结 1 623
太阳男子
太阳男子 2020-12-01 21:30

This code:

class Foo {
    std::unordered_map x;
};

gives me an error:

/usr/include/c++/4.7/bits/st         


        
相关标签:
1条回答
  • 2020-12-01 21:48

    The C++ standard specifies for the various smart pointers that the template parameter is allowed to be an incomplete type.

    This information is not given for container types. Because it is unspecified, an implementation is allowed to accept an incomplete type for one container class template and not another, and still be conformant.

    To make your code portable, avoid making containers of any type before the type is completed.

    Formally, this constraint is found in the following rule (17.6.4.8) which applies to your code:

    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.
    0 讨论(0)
提交回复
热议问题