Question:
I have a lambda that either appends an object and returns it or it returns an already existing object. On GCC, i receive the error:cannot bind non-const lvalue reference of type ‘T&’ to an rvalue of type ‘T’
Here is an example:
new_foo
is supposed to be mutated after retrieving the object, hence const foo& new_foo
is not an option.Answer:
return type of lambda is not a reference by default, you have to specify it with trailling return type(-> foo&
, -> decltype(auto)
):std::reference_wrapper
):If you have better answer, please add a comment about this, thank you!