typedef and incomplete type

后端 未结 2 1050
终归单人心
终归单人心 2020-12-11 16:52

Recently I am having many problem with typedef and incomplete type when I changed certain containers, allocators in my code.

What I had previously

st         


        
相关标签:
2条回答
  • 2020-12-11 17:40

    Compiller doesn't know the size of incomplete type, therefore it can not instantiate it nor allocate some memory for it. Having a pointer to object (like typedef std::tr1::array<foo*, 5> all_foos; ) instead of instance of the object itself solves this issue.

    0 讨论(0)
  • 2020-12-11 17:46

    A type must be complete to be used in a standard container, or the behavior is undefined (§17​.4.3.6/2). So the only standard solution is to not make that typedef until the class is defined.

    I don't get what the intermediate container is for:

    struct foo;//incomplete type.
    typedef foo& foo_ref;
    

    In any case, you'll just have to have the complete type defined first, really. To get a typedef defined in a class, that class must be instantiated, which means the entire thing must be able to use T as desired.

    For example, stack_alloc must have T be a complete type (for sizeof(T) to work), otherwise the class cannot be instantiated. If the class can't be instantiated, you cannot get the typedef out of it. Ergo, you'll never get the typedef out of it if T is incomplete.

    0 讨论(0)
提交回复
热议问题