jeudi 1 décembre 2016

Returning reference using a ternery expression causes runtime error

For some reason, the second method will not work.

concurrency::event<void( event_data& ), codec_cvt> &get_event( const string_type &event )
{
        auto it = events_.find( event );
        if ( it == events_.end( ) )
            throw /* exception */;
        return *it->second;
}

However, this method using my preffered syntax is returning an address of a temporary. When it should be exactly, correct?

concurrency::event<void( event_data& ), codec_cvt> &get_event( const string_type &event )
{
    auto it = events_.find( event );
    return it != events_.end( ) ?
        *it->second :
        throw;
 }

Aucun commentaire:

Enregistrer un commentaire