When compiling the following code with clang-3.5 to the C++11 standard:
const String& XMLAttributes::getValueAsString(const String& attrName, const String& def) const
{
return (exists(attrName)) ? getValue(attrName) : def;
}
I get the following warning:
warning:
returning reference to local temporary object [-Wreturn-stack-address]
return (exists(attrName)) ? getValue(attrName) : def;
^~~
note:
binding reference variable 'def' here
...String& attrName, const String& def) const
^
1 warning generated.
g++-4.9 does not give a similar warning.
My belief is that clang is being overzealous and that it should work fine in this case, since I know that when this function is used the input has a long enough lifetime. (I'm pretty sure I've seen a good amount of code that seems to work this way anyways...)
I'm a little bit spooked by the fact that clang says its a "local temporary object" though. Really nothing there should be a local temporary object, and if clang thinks so and is deleting things while this function is running I would like to know why.
Does the standard guarantee that the reference returned by this function (in the case where "def" is selected) will have the same lifetime as the reference that is passed in, or does it allow that they can be thought of as two different references with different lifetimes?
Aucun commentaire:
Enregistrer un commentaire