Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conflict in return type from base class with derived class using auto

Tags:

c++

c++14

I have the following code:

struct A{};

struct Base {
    virtual A& internal() = 0;
};

struct Derives : public Base {    
    auto& internal() override { // <-- conflicting return type
        return internal_;
    }

private:
    A internal_;
};

int main() {
    Derives d;
    auto& internal = d.internal();
}

This fails to compile (tested on coliru - with gcc) with a conflicting return type - my question is why can't the compiler deduce that both internal_ (and therefore the return type) is A? Is the type deduced for auto in a different stage of compilation for example than the one which checks the virtual overrides? Of course this compiles if you replace auto with the correct type - but that is besides the point.

(Here is the clang error, gcc is somewhat similar)

main.cpp:8:11: error: return type of virtual function 'internal' is not covariant with the return type of the function it overrides ('auto &' is not derived from 'A &')

auto& internal() override { // <-- conflicting return type
~~~~~ ^

main.cpp:4:16: note: overridden virtual function is here

virtual A& internal() = 0;
        ~~ ^

1 error generated.

like image 672
Nim Avatar asked Jun 09 '16 12:06

Nim


People also ask

How to define derived class with base class in C++?

In C++, we can define Derived Class with a Base Class. To do this we should use : after the name of class and we should add The type of inheritance is specified by the access-specifier and the name of base class. In general, we can define a public derived class as below,

What is the use of return type in a vector class?

It is used to be able to make polymorphic hierarchy, as vector should have homogeneous data and the only way to have hetero is through pointers of base polymorphic class (as in the example I provided). You can't do what you want. The C++ rules are that you can't overload functions on return type alone.

What is base class in Java?

The Base Class, also known as the Parent Class or the Super Class is a class, from which other classes are derived. In other term it is a base class for other derived classes. That means if a derived class which inherits the base class has all members of a base class as well as can also have some additional properties.

What is a derived class in Java?

Instead of writing completely new data members and member functions, we can designate that the new class should inherit the members of an parent class. This parent class is called as the Base Class, and the new class is called as Derived Class.


1 Answers

From [dcl.spec.auto]:

A function declared with a return type that uses a placeholder type shall not be virtual ([class.virtual]).

internal() is a virtual function, so you can't use auto.

The original proposal indicates the reasoning for this:

It would be possible to allow return type deduction for virtual functions, but that would complicate both override checking and vtable layout, so it seems preferable to prohibit this.

like image 77
Barry Avatar answered Oct 06 '22 12:10

Barry