Can’t pass temporary object as reference

By design, C++ only allows a temporary to be passed to a const reference, value, or rvalue reference. The idea is that a function taking a non-const reference parameter is stating that it wants to modify the parameter and allowing it to go back to the caller. Doing so with a temporary is meaningless and most likely an error.

And I don’t know what version of g++ you’re running. It doesn’t work here: http://coliru.stacked-crooked.com/a/43096cb398cbc973

Leave a Comment