何らかの理由で、GCCとclangの最新バージョンでは、この特定のシナリオで戻り値の共分散を認識しません。エラーメッセージは誤解を招くです:ここでは共変リターン型が認識されない
class base
{
private:
virtual base * foo() = 0;
};
template< class T >
class foo_default_impl : public virtual base
{
private:
T * foo() override { return nullptr; }
};
class derived : public virtual base, private foo_default_impl<derived>
{
};
int main() {
derived d{}; // error: return type of virtual function 'foo' is not covariant with the return type of the function it overrides ('derived *' is not derived from 'base *')
return 0;
}
'foo()'は 't *'ではなく 'foo_default_impl *'を返す必要があります。 –
以前のバージョンでコードをコンパイルしましたか?私はこれがfoo_default_implに渡されたときに 'derived'がまだ完全な型ではないのではないかと思います:http://eel.is/c++draft/class.derived#class.virtual-8 – marcinj
@KhouriGiordano:なぜ?私たちが検討している特別なケースでは、 'T'は' derived'、 'derived'は' base'から公に由来します。 –