Lambda function as a default argument for std::function in constructor

余生长醉 提交于 2019-12-05 10:28:20

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;
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!