Boost logo

Boost :

Subject: Re: [boost] [chrono] [dependence of common_type on Boost.TypeOf]
From: Adam Merz (adammerz_at_[hidden])
Date: 2010-08-15 03:44:48


vicente.botet <vicente.botet <at> wanadoo.fr> writes:
> Hi again,
> I have reached to manage with the ambiguous overloads and all the tests works
> as before.
> I have do it as follows:
<snip code>

That doesn't seem very robust in the case that there is no common type (in
terms of the compiler error anyway). Maybe I'm missing something regarding
the required semantics, but couldn't something like the following be used, so
at least you get a reasonable static assertion error instead?

#include <boost/mpl/if.hpp>
#include <boost/type_traits/is_base_of.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/static_assert.hpp>

template<typename T1, typename T2>
struct common_type
{
    typedef typename boost::mpl::if_<
        boost::is_base_of<T1, T2>,
        T1,
        typename boost::mpl::if_<
            std::is_base_of<T2, T1>,
            T2,
            void
>::type
>::type type;

    // no common type
    BOOST_STATIC_ASSERT((!boost::is_same<type, void>::value));
};

template<typename T1>
struct common_type<T1, void>
{
    // void is not a valid type
    BOOST_STATIC_ASSERT(sizeof(T1) != sizeof(T1));
};

template<typename T2>
struct common_type<void, T2>
{
    // void is not a valid type
    BOOST_STATIC_ASSERT(sizeof(T2) != sizeof(T2));
};

(Apologies for messing up the message threading. I post via Gmane, and this
thread isn't showing up there, presumably because of the encoding issues in the
message subject.)


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk