typedef and incomplete type

后端 未结 2 1065
终归单人心
终归单人心 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: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.

提交回复
热议问题