2 ms·
> What you want is actually > > auto& fooRef = myWidget.getRef(); That variant has issues if getRef() actually returns a temporary object. You may want auto&
by amluto 5y ago
> What you want is actually
>
> auto& fooRef = myWidget.getRef();
That variant has issues if getRef() actually returns a temporary object. You may want auto&&.
- Asooka 5y agoIf it returns a temporary, you'll get a compile-time error, as the temporary cannot be bound to a non-const ref. I would prefer "const auto&" as you shouldn't be modifying temporaries, or "auto x{std::move(myWidget.getRef())}" if I want a mutable value and to avoid copying the return value of getRef.