I have a very simple method, and const overload of it.
Sy_animatable::PropertyTimeLine&
Sy_animatable_imp::getPropertyTimeLine( const QString& property )
{
if ( !properties_.contains( property ) ) {
throw Sy_unknownAnimPropertyException( property );
}
return properties_[property];
}
const Sy_animatable::PropertyTimeLine&
Sy_animatable_imp::getPropertyTimeLine( const QString& property ) const
{
if ( !properties_.contains( property ) ) {
throw Sy_unknownAnimPropertyException( property );
}
return properties_[property]; // "warning: returning reference to temporary"
}
I don’t understand the warning for two reasons:
properties_is a member variable and it’s subscript operator (it’s aQMap) returns a reference, so there shouldn’t be any temporaries and it is persistant for the lifetime of the object.- Why is the warning appearing in the const overload and not the original?
I could #pragma the line to hide the warning, but I really want to know why it is giving me the warning – any suggestions?
Looks like the
[]-operator forQMaphas strange semantics that sometimes generate a const-reference to a temporary (if there’s no element with the given key), and the lifetime of that one isn’t extended far enough.Try
return properties_.find(property).value();instead.