在模板化类和非模板化类中的lambda属性中捕获它

pyr*_*yro 7 c++ lambda templates c++11 gcc4.9

我已经成功编写了一个像这样的类,在一个被定义为所述类的非静态属性的lambda中捕获它:

#include <memory>
#include <iostream>
#include <functional>

struct S
{
  S()
  {
    std::cout << "S::S()[" << this << "]" << std::endl;
  }

  std::string y_{"hi mate"};
  int x_;
  std::function<void(int*)> del_{[this](int *ptr)
  {
    std::cout << "Deleting ptr[" << ptr << "] this[" << this << "] this->y_[" << this->y_ << "]" << std::endl;
  }};
  std::unique_ptr<decltype(x_), decltype(del_)> unique_{&x_, del_};
};

int main()
{
  S s;
}
Run Code Online (Sandbox Code Playgroud)

这编译并且似乎运行得很好.

但是,对于模板化的类,它不再起作用:

#include <memory>
#include <iostream>
#include <functional>

template <typename>
struct S
{
  S()
  {
    std::cout << "S::S()[" << this << "]" << std::endl;
  }

  std::string y_{"hi mate"};
  int x_;
  std::function<void(int*)> del_{[this](int *ptr)
  {
    std::cout << "Deleting ptr[" << ptr << "] this[" << this << "] this->y_[" << this->y_ << "]" << std::endl;
  }};
  std::unique_ptr<decltype(x_), decltype(del_)> unique_{&x_, del_};
};

int main()
{
  S<int> s;
}
Run Code Online (Sandbox Code Playgroud)

$> g ++ -std = c ++ 1y custom_deleter_template.cpp
~/test custom_deleter_template.cpp:在'struct S ::'的实例化中:custom_deleter_template.cpp:9:3:需要来自'S <> :: S()[ '= custom]'custom_deleter_template.cpp:24:10:
从这里需要custom_deleter_template.cpp:15:35:内部编译器错误:在tsubst_copy中,在cp/pt.c:12569
std :: function del _ {[this](int*ptr)^如果合适,请提交完整的错误报告,并附带预处理的来源.请参阅说明.存储在/tmp/pyro/ccxfNspM.out文件中的预处理源,请将其附加到您的bug报告中.

在提交bug报告之前(我不能做,他们阻止了帐户创建),根据标准所说的,它不能编译是正常的吗?

编译器是g ++(Ubuntu 4.9.2-0ubuntu1~14.04)4.9.2,使用标志-std = c ++ 1y.标志-std = c ++ 11也会发生同样的事情.

Thi*_*aut 0

这确实是 GCC 中的一个错误,并且已经被跟踪

似乎影响4.8和4.9。正如评论中指出的,这个特定示例适用于 4.7 和 5.0。您可以在这里亲自查看并使用不同版本的 gcc。

然而,这个没有外部依赖的简化版代码在 5.0 中仍然会崩溃:

template <typename>
struct S {
  int f{[this](){return 42;}()};
};

int main(){
    return S<int>{}.f; //  should return 42
}
Run Code Online (Sandbox Code Playgroud)

我建议您在使用您的代码之前等待我引用的错误得到修复,或者切换到另一个编译器;)。