Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

returning a reference to temporary

I understand that it's illegal to return a reference to a temporary, but here's my problem:

const stringSet & Target::dirList( const dirType type ) const
{
    switch( type )
    {
        case SOURCE_DIR:
            return m_sourceDirs;
        case HEADER_DIR:
            return m_headerDirs;
        case RESOURCE_DIR:
            return m_resourceDirs;
        default:
            return stringSet(); // PROBLEM HERE!
    }
}

The three first three options return a const reference to a stringSet data member. What should I do for the default case? If I leave it out, the compiler (GCC with -Wall -Wextra -pedantic) complains and I don't want it to because those options tend to catch my bed design choices in the most odd of ways :)

Thanks!

like image 552
rubenvb Avatar asked Dec 21 '22 20:12

rubenvb


1 Answers

Keep a default set as a member too... and return a reference to it. That is of course if the default case is theoretically possible. If it is not, throw an exception and don't return anything.

default:
   throw invalid_argument_exception();
like image 104
Armen Tsirunyan Avatar answered Dec 31 '22 01:12

Armen Tsirunyan