Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a c++ trait to find the most restricted type between two types in C++?

I would like a type trait common

so that

common<int,int>::type              -> int
common<const int, int>::type       -> const int
common<int, int &>::type           -> int
common<int &, int &>::type         -> int &
common<int &, int const &>::type   -> int const &

that is the result type should be the more restricted of the two. Is there a trait in the C++11 std that can do this or do I have to roll my own?

My use case is that I have a something similar to

template <typename T0, typename T1>
struct Foo {

   BOOST_STATIC_ASSERT(
    std::is_same
    < typename std::decay<T0>::type
    , typename std::decay<T1>::type
    >::value
   );

   // I need to find T which is the most restrictive common
   // type between T0 and T1
   typedef typename common<T0,T1>::type T

   T0 t0;
   T1 t1;

   T choose(bool c){
       return c ? t0 : t1;
   }

} 
like image 605
bradgonesurfing Avatar asked Nov 21 '18 09:11

bradgonesurfing


2 Answers

I am afraid that you need to roll your own. You can warp your types in a std::tuple, then pass it to std::common_type, e.g.

#include <tuple>
#include <type_traits>

template <class T1, class T2>
struct common {
    using type = typename std::tuple_element<0, typename std::common_type<std::tuple<T1>, std::tuple<T2>>::type>::type;
};

template <class T>
struct common<const T, T> {
    using type = const T;
};

template <class T>
struct common<T, const T> {
    using type = const T;
};

template <class T>
struct common<const T, const T> {
    using type = const T;
};

int main()
{
    static_assert(std::is_same<common<int, int>::type, int>::value, "");
    static_assert(std::is_same<common<const int, int>::type, const int>::value, "");
    static_assert(std::is_same<common<int, int &>::type, int>::value, "");
    static_assert(std::is_same<common<int &, int &>::type, int &>::value, "");
    static_assert(std::is_same<common<int &, int const &>::type, int const &>::value, "");
    return 0;
}

But you have to create special cases for const.

like image 102
felix Avatar answered Nov 04 '22 22:11

felix


Taking inspiration from Oliv's solution, a possible C++11 version

#include <utility>
#include <type_traits>

template <typename T1, typename T2>
using cond_t = decltype(false ? std::declval<T1>() : std::declval<T2>());

template <typename T1, typename T2>
using common = typename std::conditional<
   std::is_reference<T1>::value || std::is_reference<T2>::value,
   cond_t<T1, T2>,
   typename std::remove_reference<cond_t<T1 &, T2 &>>::type>::type;

int main()
 {
   using t1 = common<int,int>;
   using t2 = common<const int, int>;
   using t3 = common<int, int &>;
   using t4 = common<int &, int &>;
   using t5 = common<int &, int const &>;

   static_assert( std::is_same<t1, int>::value, "!" );
   static_assert( std::is_same<t2, int const>::value, "!" );
   static_assert( std::is_same<t3, int>::value, "!" );
   static_assert( std::is_same<t4, int &>::value, "!" );
   static_assert( std::is_same<t5, int const &>::value, "!" );  
 }
like image 2
max66 Avatar answered Nov 05 '22 00:11

max66