Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inheriting from class of specialized self?

Is this valid C++?

template<class category>
class any_iterator : public any_iterator<void>
{ 
public:
        typedef any_iterator<void> any_iter_void;

        any_iterator() : any_iter_void() {}
};
template<>
class any_iterator<void>
{ 
public:
        typedef any_iterator<void> any_iter_void;

        any_iterator() {}
        void foo() {};
};

int main() {
    any_iterator<int> a;
    a.foo();
}

MSVC10 accepts it with no errors/warnings on \WALL, but gcc-4.5.1 complains:

prog.cpp:3:5: error: invalid use of incomplete type 'class any_iterator'
prog.cpp:2:11: error: declaration of 'class any_iterator'
prog.cpp: In function 'int main()':
prog.cpp:21:11: error: 'class any_iterator' has no member named 'foo'
prog.cpp: In constructor 'any_iterator::any_iterator() [with category = int]':
prog.cpp:20:27: instantiated from here
prog.cpp:7:44: error: type 'any_iterator' is not a direct base of 'any_iterator'

Can someone quote the standard showing if this should or should not compile? I think this is a bug in MSVC.

As a note, I know the correct thing to do is to declare the class, specialize the root, then define the general case, and that's what I'll do to my code, but I was wondering which compiler is wrong here?

like image 371
Mooing Duck Avatar asked Dec 07 '11 21:12

Mooing Duck


People also ask

Can a class inherit itself?

The class doesn't inherit itself. Every instatiation of Model<N> is a different, unrelated class.

Do classes inherit private members?

Private Members in a SuperclassA subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.

Can a class inherit itself python?

Inheritance allows us to define a class that inherits all the methods and properties from another class. Parent class is the class being inherited from, also called base class. Child class is the class that inherits from another class, also called derived class.

What is single inheritance in oop?

Single inheritance enables a derived class to inherit properties and behavior from a single parent class. It allows a derived class to inherit the properties and behavior of a base class, thus enabling code reusability as well as adding new features to the existing code.


1 Answers

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.

like image 81
ildjarn Avatar answered Sep 21 '22 16:09

ildjarn