与lambda一起使用虚拟继承在初始化列表中捕获此问题的GCC错误

gcc bug with lambda capture of this in initialization list with virtual inheritance?

本文关键字:问题 错误 GCC 列表 初始化 一起 lambda 虚拟 继承      更新时间:2023-10-16

gcc-4.9、5.4和6.3下的以下代码segfaults,带有std = c 11,但在clang-3.7下编译并运行良好,VS2015更新3。

struct A
{
    int Func() { return x++; }
    int x = 5;
};
struct B
{
    B(int) {}
};
struct Derived : public virtual A, public B
{
    Derived()
      : A()
      // , B(this->Func()) // This works!
      , B([this](){ return this->Func(); }()) // But this segfaults.
    {
    }
};
int main()
{
    Derived c;
}

这是GCC中的错误吗?删除虚拟继承修复了segfault。

这已作为GCC的错误提交,并已确认。

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81051