小编Vol*_*hko的帖子

无法识别协变返回类型

出于某种原因,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++

23
推荐指数
1
解决办法
730
查看次数

标签 统计

c++ ×1