简体   繁体   English

非静态数据成员初始值设定项中lambda函数的分段错误

[英]Segmentation fault for lambda function in non-static data member initializer

I am unsure about a possible GCC bug in initialization of a std::function from a lambda function capturing this in a non-static data member initializer. 我不确定在一个lambda函数初始化std::function可能存在GCC错误,该函数在非静态数据成员初始值设定项中捕获this Is this allowed by the C++ standard or is this UB? 这是允许的C ++标准还是这个UB?

Given the following code: 给出以下代码:

#include <functional>
#include <iostream>

template <typename T>
struct A {
      T x = 0;
      std::function<void(T)> f = [this](T v) { x = v; };
};

int main() {
      A<int> a;
      a.f(1);
      std::cout << a.x << "\n";
}

In my understanding, it should print 1 . 根据我的理解,它应该打印1 However, when built with GCC 5.4.0 or GCC 6.2.0, af(1) emits a segmentation fault, because the captured this pointer is null. 但是,当使用GCC 5.4.0或GCC 6.2.0构建时, af(1)会发出分段错误,因为捕获的this指针为空。

The following alternatives work as I expected: 以下备选方案按预期工作:

  • Using constructor initializer list: 使用构造函数初始化列表:

     template <typename T> struct B { B() : f([this](T v) { x = v; }) {} T x = 0; std::function<void(T)> f; }; 
  • Without template: 没有模板:

     struct C { int x = 0; std::function<void(int)> f = [this](int v) { x = v; }; }; 

Also, when built with Clang 3.8.0, all three versions behave as I expect, which doesn't mean it is not UB. 此外,当使用Clang 3.8.0构建时,所有三个版本都按照我的预期运行,这并不意味着它不是UB。

You cannot do: 你做不到:

template <typename T>
struct A {
    T x = 0;
    std::function<void(T)> f = [this](T v) { x = v; };
};

As this does not exist when you define f . 由于this ,当你定义不存在f You need to initilize f in a constructor, such as: 您需要在构造函数中初始化f ,例如:

A(){ f = [this](T v){ x=v; } }

It worked with G++4.8. 它适用于G ++ 4.8。

Your code compiles and runs on VS2015 (windows). 您的代码在VS2015(windows)上编译并运行。 So this is probably a compiler error. 所以这可能是一个编译器错误。

Also, if you will remove the template it works on http://cpp.sh/ Try this code: 此外,如果您将删除它在http://cpp.sh/上工作的模板,请尝试以下代码:

#include <functional>
#include <iostream>

struct A {
      int x = 0;
      std::function<void(int)> f = [this](int v) { x = v; };
};

int main() {
      A  a;
      a.f(1);
      std::cout << a.x << "\n";
}

running original code on cpp.sh gives: 在cpp.sh上运行原始代码给出:

 internal compiler error: in tsubst_copy, at cp/pt.c:12569
Please submit a full bug report

So I guess it's a bug 所以我猜这是一个错误

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否允许在默认成员初始化程序中调用非静态成员 function? - Is it allowed to call a non-static member function in a default member initializer? 当我们引用非静态数据成员时,分段错误是否是实际未定义的行为 - Is segmentation fault actual undefined behavior when we refer to a non-static data-member 成员初始值设定项不命名非静态数据成员 - Member initializer does not name a non-static data member 部分聚合初始化和非静态数据成员初始化程序 - Partial Aggregate Initialization and Non-static Data Member Initializer 来自另一个非静态的非静态成员初始化程序 - Non-static member initializer from another non-static 成员初始化程序列表和非静态数据成员上的默认成员初始值设定项之间的区别是什么? - What's the differences between member initializer list and default member initializer on non-static data member? 当初始值设定项是基类名称时出现错误“初始值设定项未命名非静态数据成员或基类” - Error 'initializer does not name a non-static data member or base class' when the initializer is the base class name 成员初始化程序“ SuperClass”未命名非静态数据成员或基类 - member initializer 'SuperClass' does not name a non-static data member or base class 静态回调函数和非静态成员 - Static callback function and non-static member 功能指向非静态成员函数的指针? - Function Pointer to a non-static member function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM