Error with lambda in the operator of the << class template with default argument

Can someone enlighten me what is wrong with this code:

template<typename B=int> // <<<< =int is provoking the error
struct Foo
{
    std::array<B,10> data;

    template<typename F>
    void iterate(F f) const
    {
        for(unsigned i=0; i<10; i++) {
            f(i, data[i]);
        }
    }

    friend std::ostream& operator<<(std::ostream& os, const Foo<B>& a) // Line 17
    {
        a.iterate([&os](unsigned i, const B& x) {
            os << i << "=" << x << "\n";
        });
        return os;
    }
};

Error message with GCC 4.8.1 and --std=c++11:

test.cpp: In function ‘std::ostream& operator<<(std::ostream&, const Foo<B>&)’:
test.cpp:17:41: error: default argument for template parameter for class enclosing ‘operator<<(std::ostream&, const Foo<B>&)::__lambda0’
   a.iterate([&os](unsigned i, const B& x) {
+4
source share
1 answer
a.iterate([&os](unsigned i, const B& x) {
    os << i << "=" << x << "\n";
});

The lambda function presented here has the effect of declaring an "unnamed" local class in the function region inside the function body operator<<. We can write our own functor object to get a similar effect:

struct lambda {
    std::ostream& os;

    lambda(std::ostream& os_) : os(os_) {}

    void operator()(unsigned i, const B& x) {
        os << i << "=" << x << "\n";
    }
};
a.iterate(lambda(os));

This causes a similar error from g ++:

error: default argument for template parameter for class enclosing
       ‘operator<<(std::ostream&, const Foo<B>&)::lambda::lambda’

Here is the SSCCE:

template <typename T = int>
struct Foo
{
    friend void f() {
        struct local {};
    }
};

And the error it causes is:

error: default argument for template parameter for class enclosing
       ‘f()::local::local’

This has already been reported as bug GCC 57775 .

@PiotrNycz, , operator<< .

+1

Source: https://habr.com/ru/post/1537265/


All Articles