我已经成功编写了一个像这样的类,在一个被定义为所述类的非静态属性的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; }
这编译并且似乎运行得很好.
但是,对于模板化的类,它不再起作用:
#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_),del_}; }; int main() { S<int> s; }
$> g++ -std=c++1y custom_deleter_template.cpp
~/test custom_deleter_template.cpp: In instantiation of ‘struct
S::’: custom_deleter_template.cpp:9:3: required
from ‘S< >::S() [with
= int]’ custom_deleter_template.cpp:24:10:
required from here custom_deleter_template.cpp:15:35: internal
compiler error: in tsubst_copy,at cp/pt.c:12569
std::function del_{[this](int *ptr)
^ Please submit a full bug report,with preprocessed source if appropriate. See
for instructions.
Preprocessed source stored into /tmp/pyro/ccxfNspM.out file,please
attach this to your bugreport.
在提交bug报告之前(我不能做,他们阻止了帐户创建),根据标准所说的,它不能编译是正常的吗?
编译器是g(Ubuntu 4.9.2-0ubuntu1~14.04)4.9.2,使用标志-std = c 1y.标志-std = c 11也会发生同样的事情.