Search code examples
c++language-lawyerpure-virtualtrailing-return-type

Can abstract types be used as the return type of a pure virtual function?


If I understand correctly, abstract types cannot be used as the return type of a pure virtual function. However, if a trailing return type is used, then gcc (but not clang) accepts the following code:

struct S {
    virtual auto f() -> S = 0; 
};

demo.

Is this a gcc bug, or does the language not require a diagnostic to be issued for this code?


Solution

  • According to C++17 [class.abstract]/3, an abstract type cannot be used as the return type of any function:

    An abstract class shall not be used as a parameter type, as a function return type, or as the type of an explicit conversion. Pointers and references to an abstract class can be declared. [ Example:

    shape x;           // error: object of abstract class
    shape* p;          // OK
    shape f();         // error
    void g(shape);     // error
    shape& h(shape&);  // OK
    

    end example ]

    There doesn't seem to be any special exception for pure virtual functions. Therefore, it seems that the answer is that the compiler is required to issue a diagnostic for your code.