【发布时间】:2015-02-15 22:57:11
【问题描述】:
我已经成功编写了一个像这样的类,在定义为所述类的非静态属性的 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_), decltype(del_)> unique_{&x_, del_};
};
int main()
{
S<int> s;
}
$> g++ -std=c++1y custom_deleter_template.cpp
~/test custom_deleter_template.cpp:在'struct的实例化中 S::': custom_deleter_template.cpp:9:3: 必需 来自'S::S() [与 = int]' custom_deleter_template.cpp:24:10:
此处需要 custom_deleter_template.cpp:15:35: internal 编译器错误:在 tsubst_copy 中,位于 cp/pt.c:12569
std::function del_{[this](int *ptr) ^ 请提交完整的错误报告,并在适当的情况下提供预处理的源代码。看 获取说明。 预处理源存储到 /tmp/pyro/ccxfNspM.out 文件中,请 将此附加到您的错误报告中。
在提交错误报告之前(我不能这样做,他们阻止了帐户创建),根据标准的规定,它不编译是否正常?
编译器是 g++ (Ubuntu 4.9.2-0ubuntu1~14.04) 4.9.2,使用标志 -std=c++1y。标志 -std=c++11 也会发生同样的情况。
【问题讨论】:
-
内部编译器错误始终是一个错误。这应该像在clang 中那样编译。
-
我可以在 OS X 上使用 GCC 4.9.2 重现。
-
它确实在 clang 中编译 :) .
-
它还使用更新的 g++5.0 编译,所以它可能是一个已经修复的错误。
-
@dyp :您介意提供一个链接以在 linux 上获取 g++5.0 吗?我检查的唯一镜像只有 4.9.x 版本。
标签: c++ templates c++11 lambda gcc4.9