简体   繁体   中英

what should I return if I don't have something to return for an unknown type

For the following code, what should I return if I don't have something to return ? Currently, I do something like T() but I'm not sure it's correct and proper.

template<typename T1, typename T2>
tuple<T1, T2, bool> CBR<T1, T2>::getSomething(T1 t)
{
    // here I I don't have something of time T2 or T2 to return
    if( ... )
        return make_tuple(T1(), T2(), false); // @FIXME

    if( ... )
        return make_tuple(something.tp, something.ts, false);
    else
        return make_tuple(something.tp, something.ts, true);
}
boost::optional<tuple<T1, T2, bool> >

The "good" return paths will be unchanged; the "FIXME" one will become:

return boost::none;

This can allow your class to be used with types that do not support default construction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM