Is it possible to return a literal by const reference?

It's safe?

const int& f() { return 1; } 

What I'm trying to do is return some value to const &

+7
source share
2 answers

This not normal.
Returning a link to a temporary one is not true, because accessing it outside the function causes Undefined Behavior .

+8
source

No, you are returning a link to a temporary variable - this is unsafe. The temporary variable will be destroyed when the function returns.

+2
source

All Articles