Q:

C++ "warning: returning reference to temporary " - But it isn't

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 a QMap) 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?

A:

Looks like the []-operator for QMap has 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.

