inheriting from class of specialized self?

后端 未结 2 1533
别那么骄傲
别那么骄傲 2021-01-19 00:43

Is this valid C++?

template
class any_iterator : public any_iterator
{ 
public:
        typedef any_iterator an         


        
相关标签:
2条回答
  • 2021-01-19 01:21

    To inherit from a type, that type must be complete. A little rearranging solves things:

    template<class category>
    class any_iterator;
    
    template<>
    class any_iterator<void>
    { 
    public:
        typedef any_iterator<void> any_iter_void;
    
        any_iterator() { }
        void foo() { }
    };
    
    template<class category>
    class any_iterator : public any_iterator<void>
    { 
    public:
        typedef any_iterator<void> any_iter_void;
    
        any_iterator() : any_iter_void() { }
    };
    
    int main()
    {
        any_iterator<int> a;
        a.foo();
    }
    

    Token standard quotes:

    C++11, §10/2:

    The type denoted by a base-type-specifier shall be a class type that is not an incompletely defined class; this class is called a direct base class for the class being defined.

    §9.2/2:

    A class is considered a completely-defined object type (or complete type) at the closing } of the class-specifier.

    0 讨论(0)
  • 2021-01-19 01:26

    10/2:

    The type denoted by a base-type-specifier shall be a class type that is not an incompletely defined class

    It is one manifestation of the bug of MSVC: its lack of two-phase name resolution.

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