1
template <typename T, typename Y, typename... Args>
class Bar
{
    T& t;
public:
    Bar(T& t) : t(t) { }
};

template <typename T, typename... Args>
void Foo(T &function) { new Bar<T, void, Args...>(function); }

int main()
{
    auto foo = [] { };
    Foo(foo); // ok

    Foo([] { }); // fails (tested on GCC 4.5.3)
}

Why does it only fail when the lambda expression is written inline as an argument to Foo?

1 Answer 1

5
template <typename T, typename... Args>
void Foo(T &function) { new Bar<T, void, Args...>(function); }

Foo([] { }); // fails (tested on GCC 4.5.3)

Lambda is temporary. Don't try bind temporary to reference. Use value, or const-reference or rvalue-reference.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.