• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Cannot assign reference value to result of std::invoke

Resolved: Cannot assign reference value to result of std::invoke

0
By Isaac Tonny on 18/06/2022 Issue
Share
Facebook Twitter LinkedIn

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:
In this 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)):
or return a type which handles reference (as std::reference_wrapper):
Demo

If you have better answer, please add a comment about this, thank you!

c++ lambda lvalue rvalue
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: EntityFramework creates/runs migrations using parameterless DataContext instance

24/03/2023

Resolved: Visual Studio 2022 crashes when using breakpoints

24/03/2023

Resolved: How to get Union type from an array of objects in Flow?

24/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.