Lambda函数作为构造函数中std :: function的默认参数 [英] Lambda function as a default argument for std::function in constructor
问题描述
我想在类的构造函数中有一个functor参数的默认函子。作为一个最小的例子,我想出了一个类,应该服务器作为过滤器,它过滤类型 T
iif一个过滤器函数返回true的元素。过滤器函数应该在构造函数中提供,默认为接受所有过滤器函数:
template< class T&
class Filter
{
public:
typedef std :: function< bool(const T&)> FilterFunc;
Filter(const FilterFunc& f = [](const T&){return true;}):
f(f)
{
} $ b b
private:
FilterFunc f;
};
我像下面这样实例化模板类:
int main(){
Filter< int> someInstance; //不提供过滤器功能(< - 第19行)
}
prog.cpp:19:17:internal compiler error:in tsubst_copy,at cp / pt.c: 12141
如果合适,请提交一份完整的错误报告,
,使用预处理源。
参见< file:///usr/share/doc/gcc-4.7/README.Bugs>以获取说明。
预处理的源存储到/home/g9i3n9/cc82xcqE.out文件中,请将其附加到您的错误报告。
有什么问题?是我的代码标准符合(所以GCC是真的在这里或者没有实现这个)或者我做错了什么?
作为解决方法,我目前使用默认构造的 std :: function
并且只有当它被设置时才调用它(我想调用它):
Filter(const FilterFunc& f = FilterFunc):
f(f)
{
}
//使用它:
void process(){
if(!f || f(someItem)){//< - workaround
}
}
这是一个错误:编译器崩溃或发生致命的内部错误,源代码,并且邮件本身诚挚地邀请您对错误进行处理:
请提交完整的错误报告,
这是另一种可能的解决方法:
template< class T>
class Filter
{
public:
typedef std :: function< bool(const T&)> FilterFunc;
Filter(){}
Filter(FilterFunc const& f):f(f){}
private:
FilterFunc f = (const T&){return true; };
};
作为另一种选择,GCC支持委托构造函数 考虑:
#include< functional>
template< class T>
class Filter
{
public:
typedef std :: function< bool(const T&)> FilterFunc;
Filter():Filter([](const T&){return true;}){}
Filter(FilterFunc const& f):f b
private:
FilterFunc f;
};
I'd like to have a default functor for a functor parameter in the constructor of a class. As a minimal example I came up with a class which should server as a filter, which filters elements of type T
iif a filter function returns true. The filter function should be provided in the constructor, defaulting to an "accept all" filter function:
template<class T>
class Filter
{
public:
typedef std::function<bool(const T&)> FilterFunc;
Filter(const FilterFunc & f = [](const T&){ return true; }) :
f(f)
{
}
private:
FilterFunc f;
};
I instantiate the template class like the following:
int main() {
Filter<int> someInstance; // No filter function provided (<-- line 19)
}
However, gcc 4.7 doesn't seem to like this piece of code:
prog.cpp: In constructor ‘Filter<T>::Filter(const FilterFunc&) [with T = int; Filter<T>::FilterFunc = std::function<bool(const int&)>]’:
prog.cpp:19:17: internal compiler error: in tsubst_copy, at cp/pt.c:12141
Please submit a full bug report,
with preprocessed source if appropriate.
See <file:///usr/share/doc/gcc-4.7/README.Bugs> for instructions.
Preprocessed source stored into /home/g9i3n9/cc82xcqE.out file, please attach this to your bugreport.
What's wrong? Is my code standard conformant (so GCC is really buggy here or hasn't implemented this) or am I doing something wrong?
As a workaround, I currently use a default-constructed std::function
and only call it (where I want to call it) if it was set:
Filter(const FilterFunc & f = FilterFunc) :
f(f)
{
}
// When using it:
void process() {
if (!f || f(someItem)) { // <-- workaround
}
}
This is a bug: the compiler crashed or a fatal internal error occurred while processing the source code, and the message itself is kindly inviting you to treat the error as such:
"Please submit a full bug report, with preprocessed source if appropriate."
Here is another possible workaround:
template<class T>
class Filter
{
public:
typedef std::function<bool(const T&)> FilterFunc;
Filter() { }
Filter(FilterFunc const& f) : f(f) { }
private:
FilterFunc f = [](const T&){ return true; };
};
As a further alternative, GCC supports delegating constructors, which you may want to consider:
#include <functional>
template<class T>
class Filter
{
public:
typedef std::function<bool(const T&)> FilterFunc;
Filter() : Filter([](const T&){ return true; }) { }
Filter(FilterFunc const& f) : f(f) { }
private:
FilterFunc f;
};
这篇关于Lambda函数作为构造函数中std :: function的默认参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!