const std::string s1 = "abc";
const std::string & s2 = "abc";
Is the definition of s2
legal? If so, what is the difference between between s1
and s2
?
Thanks.
Yes, s2 is legal. s2 is bound to a temporary std::string - extending the temporaries lifetime. s1 isn't a temporary, it's a named variable.
See 12.2/5:
The second context is when a reference is bound to a temporary. The temporary to which the reference is bound or the temporary that is the complete object to a subobject of which the temporary is bound persists for the lifetime of the reference except as specified below.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With