Covariant return types, const-ness, and incomplete classes

后端 未结 2 527
耶瑟儿~
耶瑟儿~ 2021-02-12 14:39

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         


        
2条回答
  •  名媛妹妹
    2021-02-12 15:30

    I also find nothing wrong with your code. It compiles with the head version of clang and all compilers I tried, except clang 3.8 and earlier.

    Live demo

    The relevant standard text:

    10.3 note 8:

    If the class type in the covariant return type of D::f differs from that of B::f, the class type in the return type of D::f shall be complete at the point of declaration of D::f or shall be the class type D.

    The class type of the covariant methods must be the same or complete, but as I understand it const/volatile differences are still considered the same class type, making your example legal.

提交回复
热议问题