This code compiles successfully under g++ 6.1 but gives an error with clang 3.8:
class C;
class Base {
public:
virtual const C *getC();
};
class Derived : pu
This is clang 3.8 bug, specifically 26297. From [class.virtual], wording from N4594:
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::foverrides a functionB::f, the return types of the functions are covariant if they satisfy the following criteria: (7.1) — both are pointers to classes, both are lvalue references to classes, or both are rvalue references to classes
(7.2) — the class in the return type ofB::fis 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
(7.3) — both pointers or references have the same cv-qualification and the class type in the return type ofD::fhas the same cv-qualification as or less cv-qualification than the class type in the return type ofB::f.
Having B::f return C const* and D::f return C* matches all of these requirements (neither pointer is cv-qualified, and the class type of D::f is less cv-qualified than the base), hence it should be allowed.
There is no requirement on completeness; C does not need to be complete to check that these criteria.