I have a function auto function(...)
. Is it possible to return a reference from it?
I think using a reference wrapper (std::ref
?) would be the right thing to do, but I didn't really get how it works and is used, so please be so kind a write a short explanation.
I am aware of the dangers of returning references, but in my case it would make things easier if I could.
Sure, use auto&
, much like as you would in a for
loop if you were too busy to write out the type explicitly and didn't want to take value copies:
auto& nearly_ub() {
static int foo;
return foo;
}
but beware the dangling reference possibility.