Home | History | Annotate | Download | only in meta.trans.other
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // type_traits
     11 
     12 // common_type
     13 
     14 #include <type_traits>
     15 
     16 #include "test_macros.h"
     17 
     18 struct E {};
     19 
     20 template <class T>
     21 struct X { explicit X(T const&){} };
     22 
     23 template <class T>
     24 struct S { explicit S(T const&){} };
     25 
     26 namespace std
     27 {
     28     template <typename T>
     29     struct common_type<T, ::S<T> >
     30     {
     31         typedef S<T> type;
     32     };
     33 }
     34 
     35 #if TEST_STD_VER >= 11
     36 template <class T, class U, class = void>
     37 struct no_common_type : std::true_type {};
     38 
     39 template <class T, class U>
     40 struct no_common_type<T, U, typename std::conditional<false,
     41     typename std::common_type<T, U>::type, void>::type> : std::false_type {};
     42 #endif // TEST_STD_VER >= 11
     43 
     44 int main()
     45 {
     46     static_assert((std::is_same<std::common_type<int>::type, int>::value), "");
     47     static_assert((std::is_same<std::common_type<char>::type, char>::value), "");
     48 #if TEST_STD_VER > 11
     49     static_assert((std::is_same<std::common_type_t<int>,   int>::value), "");
     50     static_assert((std::is_same<std::common_type_t<char>, char>::value), "");
     51 #endif
     52 
     53     static_assert((std::is_same<std::common_type<               int>::type, int>::value), "");
     54     static_assert((std::is_same<std::common_type<const          int>::type, int>::value), "");
     55     static_assert((std::is_same<std::common_type<      volatile int>::type, int>::value), "");
     56     static_assert((std::is_same<std::common_type<const volatile int>::type, int>::value), "");
     57 
     58     static_assert((std::is_same<std::common_type<int,           int>::type, int>::value), "");
     59     static_assert((std::is_same<std::common_type<int,     const int>::type, int>::value), "");
     60 
     61     static_assert((std::is_same<std::common_type<long,       const int>::type, long>::value), "");
     62     static_assert((std::is_same<std::common_type<const long,       int>::type, long>::value), "");
     63     static_assert((std::is_same<std::common_type<long,    volatile int>::type, long>::value), "");
     64     static_assert((std::is_same<std::common_type<volatile long,    int>::type, long>::value), "");
     65     static_assert((std::is_same<std::common_type<const long, const int>::type, long>::value), "");
     66 
     67     static_assert((std::is_same<std::common_type<double, char>::type, double>::value), "");
     68     static_assert((std::is_same<std::common_type<short, char>::type, int>::value), "");
     69 #if TEST_STD_VER > 11
     70     static_assert((std::is_same<std::common_type_t<double, char>, double>::value), "");
     71     static_assert((std::is_same<std::common_type_t<short, char>, int>::value), "");
     72 #endif
     73 
     74     static_assert((std::is_same<std::common_type<double, char, long long>::type, double>::value), "");
     75     static_assert((std::is_same<std::common_type<unsigned, char, long long>::type, long long>::value), "");
     76 #if TEST_STD_VER > 11
     77     static_assert((std::is_same<std::common_type_t<double, char, long long>, double>::value), "");
     78     static_assert((std::is_same<std::common_type_t<unsigned, char, long long>, long long>::value), "");
     79 #endif
     80 
     81     static_assert((std::is_same<std::common_type<               void>::type, void>::value), "");
     82     static_assert((std::is_same<std::common_type<const          void>::type, void>::value), "");
     83     static_assert((std::is_same<std::common_type<      volatile void>::type, void>::value), "");
     84     static_assert((std::is_same<std::common_type<const volatile void>::type, void>::value), "");
     85 
     86     static_assert((std::is_same<std::common_type<void,       const void>::type, void>::value), "");
     87     static_assert((std::is_same<std::common_type<const void,       void>::type, void>::value), "");
     88     static_assert((std::is_same<std::common_type<void,    volatile void>::type, void>::value), "");
     89     static_assert((std::is_same<std::common_type<volatile void,    void>::type, void>::value), "");
     90     static_assert((std::is_same<std::common_type<const void, const void>::type, void>::value), "");
     91 
     92 #if TEST_STD_VER >= 11
     93     static_assert((no_common_type<void, int>::value), "");
     94     static_assert((no_common_type<int, void>::value), "");
     95     static_assert((no_common_type<int, E>::value), "");
     96     static_assert((no_common_type<int, X<int> >::value), "");
     97 #endif // TEST_STD_VER >= 11
     98 
     99     static_assert((std::is_same<std::common_type<int, S<int> >::type, S<int> >::value), "");
    100     static_assert((std::is_same<std::common_type<int, S<int>, S<int> >::type, S<int> >::value), "");
    101     static_assert((std::is_same<std::common_type<int, int, S<int> >::type, S<int> >::value), "");
    102 }
    103