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

I would like to have a default functor for the functor parameter in the class constructor. As a minimal example, I came up with a class that should be a server as a filter that filters elements of type Tiif the filter function returns true. The filter function must be specified in the constructor, the default is to accept all:

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 create an instance of the template class as follows:

int main() {
    Filter<int> someInstance;  // No filter function provided    (<-- line 19)
}

However, gcc 4.7 does not look 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 happened? Is my standard a standard code (so does GCC really buggy here or didn't implement this), or am I doing something wrong?

std::function (, ), :

    Filter(const FilterFunc & f = FilterFunc) :
        f(f)
    {
    }

    // When using it:
    void process() {
        if (!f || f(someItem)) {    // <-- workaround
        }
    }
+5
1

: , :

", , ".

:

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(f) { }

private:
    FilterFunc f;
};
+3

All Articles