出于某种原因,GCC和clang的最新版本在此特定方案中无法识别返回类型协方差.错误消息具有误导性:
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 *')
Run Code Online (Sandbox Code Playgroud)
这是代码:
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: …Run Code Online (Sandbox Code Playgroud) c++ ×1