The following code compiles and runs, and no warning is emitted by either gcc or clang:
#include <iostream>
struct Base {
virtual ~Base() = default;
virtual std::string const& get() = 0;
};
struct Derived: Base {
virtual std::string& get() override { return m; }
std::string m;
};
int main()
{
Derived d;
d.get() = "Hello, World";
Base& b = d;
std::cout << b.get() << "\n";
}
Is std::string&
covariant with std::string const&
then?
Yes
This is specified in class.virtual , in the latest draft (n4606) we see:
§10.3 7/ The return type of an overriding function shall be either identical to the return type of the overridden function or covariant with the classes of the functions. If a function
D::f
overrides a functionB::f
, the return types of the functions are covariant if they satisfy the following criteria:
- both are pointers to classes, both are lvalue references to classes, or both are rvalue references to classes 111
- the class in the return type of
B::f
is the same class as the class in the return type ofD::f
, or is an unambiguous and accessible direct or indirect base class of the class in the return type ofD::f
- both pointers or references have the same cv-qualification and the class type in the return type of
D::f
has the same cv-qualification as or less cv-qualification than the class type in the return type ofB::f
.
Specifically, the last point addresses exactly the case here: it is acceptable for an overriding type to lose the const
and/or volatile
qualifiers (it cannot, however, gain them).
Note: as mentioned by @george above, paragraph 8/ used to prevent this from working with incomplete class types, but this was since fixed .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.