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

151 Views Asked by At

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);
}
1

There are 1 best solutions below

3
On
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.