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 // <functional>
     11 
     12 // result_of<Fn(ArgTypes...)>
     13 
     14 #include <type_traits>
     15 #include <memory>
     16 #include <cassert>
     17 #include "test_macros.h"
     18 
     19 struct S
     20 {
     21     typedef short (*FreeFunc)(long);
     22     operator FreeFunc() const;
     23     double operator()(char, int&);
     24     double const& operator()(char, int&) const;
     25     double volatile& operator()(char, int&) volatile;
     26     double const volatile& operator()(char, int&) const volatile;
     27 };
     28 
     29 
     30 struct SD : public S { };
     31 
     32 struct NotDerived {};
     33 
     34 template <class Tp>
     35 struct Voider {
     36     typedef void type;
     37 };
     38 
     39 template <class T, class = void>
     40 struct HasType : std::false_type {};
     41 
     42 template <class T>
     43 struct HasType<T, typename Voider<typename T::type>::type> : std::true_type {};
     44 
     45 #if TEST_STD_VER > 14
     46 template <typename T, typename U>
     47 struct test_invoke_result;
     48 
     49 template <typename Fn, typename ...Args, typename Ret>
     50 struct test_invoke_result<Fn(Args...), Ret>
     51 {
     52     static void call()
     53     {
     54         static_assert(std::is_invocable<Fn, Args...>::value, "");
     55         static_assert(std::is_invocable_r<Ret, Fn, Args...>::value, "");
     56         static_assert((std::is_same<typename std::invoke_result<Fn, Args...>::type, Ret>::value), "");
     57     }
     58 };
     59 #endif
     60 
     61 template <class T, class U>
     62 void test_result_of()
     63 {
     64     static_assert((std::is_same<typename std::result_of<T>::type, U>::value), "");
     65 #if TEST_STD_VER > 14
     66     test_invoke_result<T, U>::call();
     67 #endif
     68 }
     69 
     70 #if TEST_STD_VER > 14
     71 template <typename T>
     72 struct test_invoke_no_result;
     73 
     74 template <typename Fn, typename ...Args>
     75 struct test_invoke_no_result<Fn(Args...)>
     76 {
     77     static void call()
     78     {
     79         static_assert(std::is_invocable<Fn, Args...>::value == false, "");
     80         static_assert((!HasType<std::invoke_result<Fn, Args...> >::value), "");
     81     }
     82 };
     83 #endif
     84 
     85 template <class T>
     86 void test_no_result()
     87 {
     88 #if TEST_STD_VER >= 11
     89     static_assert((!HasType<std::result_of<T> >::value), "");
     90 #endif
     91 #if TEST_STD_VER > 14
     92     test_invoke_no_result<T>::call();
     93 #endif
     94 }
     95 
     96 int main()
     97 {
     98     typedef NotDerived ND;
     99     { // functor object
    100     test_result_of<S(int), short> ();
    101     test_result_of<S&(unsigned char, int&), double> ();
    102     test_result_of<S const&(unsigned char, int&), double const &> ();
    103     test_result_of<S volatile&(unsigned char, int&), double volatile&> ();
    104     test_result_of<S const volatile&(unsigned char, int&), double const volatile&> ();
    105     }
    106     { // pointer to function
    107     typedef bool         (&RF0)();
    108     typedef bool*       (&RF1)(int);
    109     typedef bool&       (&RF2)(int, int);
    110     typedef bool const& (&RF3)(int, int, int);
    111     typedef bool        (*PF0)();
    112     typedef bool*       (*PF1)(int);
    113     typedef bool&       (*PF2)(int, int);
    114     typedef bool const& (*PF3)(int, int, int);
    115     typedef bool        (*&PRF0)();
    116     typedef bool*       (*&PRF1)(int);
    117     typedef bool&       (*&PRF2)(int, int);
    118     typedef bool const& (*&PRF3)(int, int, int);
    119     test_result_of<RF0(), bool>();
    120     test_result_of<RF1(int), bool*>();
    121     test_result_of<RF2(int, long), bool&>();
    122     test_result_of<RF3(int, long, int), bool const&>();
    123     test_result_of<PF0(), bool>();
    124     test_result_of<PF1(int), bool*>();
    125     test_result_of<PF2(int, long), bool&>();
    126     test_result_of<PF3(int, long, int), bool const&>();
    127     test_result_of<PRF0(), bool>();
    128     test_result_of<PRF1(int), bool*>();
    129     test_result_of<PRF2(int, long), bool&>();
    130     test_result_of<PRF3(int, long, int), bool const&>();
    131     }
    132     { // pointer to member function
    133 
    134     typedef int         (S::*PMS0)();
    135     typedef int*        (S::*PMS1)(long);
    136     typedef int&        (S::*PMS2)(long, int);
    137     test_result_of<PMS0(                             S),   int> ();
    138     test_result_of<PMS0(                             S&),  int> ();
    139     test_result_of<PMS0(                             S*),  int> ();
    140     test_result_of<PMS0(                             S*&), int> ();
    141     test_result_of<PMS0(      std::reference_wrapper<S>),  int> ();
    142     test_result_of<PMS0(const std::reference_wrapper<S>&), int> ();
    143     test_result_of<PMS0(      std::reference_wrapper<SD>),  int> ();
    144     test_result_of<PMS0(const std::reference_wrapper<SD>&), int> ();
    145     test_result_of<PMS0(std::unique_ptr<S>),  int> ();
    146     test_result_of<PMS0(std::unique_ptr<SD>), int> ();
    147     test_no_result<PMS0(const          S&)>();
    148     test_no_result<PMS0(volatile       S&)>();
    149     test_no_result<PMS0(const volatile S&)>();
    150     test_no_result<PMS0(ND &                           )>();
    151     test_no_result<PMS0(const ND&                      )>();
    152     test_no_result<PMS0(std::unique_ptr<S const>       )>();
    153     test_no_result<PMS0(std::reference_wrapper<S const>)>();
    154     test_no_result<PMS0(std::reference_wrapper<ND>     )>();
    155     test_no_result<PMS0(std::unique_ptr<ND>            )>();
    156 
    157     test_result_of<PMS1(                             S,   int), int*> ();
    158     test_result_of<PMS1(                             S&,  int), int*> ();
    159     test_result_of<PMS1(                             S*,  int), int*> ();
    160     test_result_of<PMS1(                             S*&, int), int*> ();
    161     test_result_of<PMS1(std::unique_ptr<S>,               int), int*> ();
    162     test_result_of<PMS1(std::unique_ptr<SD>,              int), int*> ();
    163     test_result_of<PMS1(std::reference_wrapper<S>,        int), int*> ();
    164     test_result_of<PMS1(const std::reference_wrapper<S>&, int), int*> ();
    165     test_result_of<PMS1(std::reference_wrapper<SD>,        int), int*> ();
    166     test_result_of<PMS1(const std::reference_wrapper<SD>&, int), int*> ();
    167     test_no_result<PMS1(const          S&, int)>();
    168     test_no_result<PMS1(volatile       S&, int)>();
    169     test_no_result<PMS1(const volatile S&, int)>();
    170     test_no_result<PMS1(ND &,                            int)>();
    171     test_no_result<PMS1(const ND&,                       int)>();
    172     test_no_result<PMS1(std::unique_ptr<S const>,        int)>();
    173     test_no_result<PMS1(std::reference_wrapper<S const>, int)>();
    174     test_no_result<PMS1(std::reference_wrapper<ND>,      int)>();
    175     test_no_result<PMS1(std::unique_ptr<ND>,             int)>();
    176 
    177     test_result_of<PMS2(               S,   int, int), int&> ();
    178     test_result_of<PMS2(               S&,  int, int), int&> ();
    179     test_result_of<PMS2(               S*,  int, int), int&> ();
    180     test_result_of<PMS2(               S*&, int, int), int&> ();
    181     test_result_of<PMS2(std::unique_ptr<S>, int, int), int&> ();
    182     test_result_of<PMS2(std::unique_ptr<SD>, int, int), int&> ();
    183     test_result_of<PMS2(std::reference_wrapper<S>,         int, int), int&> ();
    184     test_result_of<PMS2(const std::reference_wrapper<S>&,  int, int), int&> ();
    185     test_result_of<PMS2(std::reference_wrapper<SD>,        int, int), int&> ();
    186     test_result_of<PMS2(const std::reference_wrapper<SD>&, int, int), int&> ();
    187     test_no_result<PMS2(const          S&, int, int)>();
    188     test_no_result<PMS2(volatile       S&, int, int)>();
    189     test_no_result<PMS2(const volatile S&, int, int)>();
    190     test_no_result<PMS2(std::unique_ptr<S const>,   int, int)>();
    191     test_no_result<PMS2(std::reference_wrapper<S const>, int, int)>();
    192     test_no_result<PMS2(const ND&,                  int, int)>();
    193     test_no_result<PMS2(std::reference_wrapper<ND>, int, int)>();
    194     test_no_result<PMS2(std::unique_ptr<ND>,        int, int)>();
    195 
    196     typedef int        (S::*PMS0C)() const;
    197     typedef int*       (S::*PMS1C)(long) const;
    198     typedef int&       (S::*PMS2C)(long, int) const;
    199     test_result_of<PMS0C(               S),   int> ();
    200     test_result_of<PMS0C(               S&),  int> ();
    201     test_result_of<PMS0C(const          S&),  int> ();
    202     test_result_of<PMS0C(               S*),  int> ();
    203     test_result_of<PMS0C(const          S*),  int> ();
    204     test_result_of<PMS0C(               S*&), int> ();
    205     test_result_of<PMS0C(const          S*&), int> ();
    206     test_result_of<PMS0C(std::unique_ptr<S>), int> ();
    207     test_result_of<PMS0C(std::unique_ptr<SD>), int> ();
    208     test_result_of<PMS0C(std::reference_wrapper<S>              ), int> ();
    209     test_result_of<PMS0C(std::reference_wrapper<const S>        ), int> ();
    210     test_result_of<PMS0C(const std::reference_wrapper<S> &      ), int> ();
    211     test_result_of<PMS0C(const std::reference_wrapper<const S> &), int> ();
    212     test_result_of<PMS0C(std::reference_wrapper<SD>             ), int> ();
    213     test_result_of<PMS0C(std::reference_wrapper<const SD>       ), int> ();
    214     test_result_of<PMS0C(const std::reference_wrapper<SD> &     ), int> ();
    215     test_result_of<PMS0C(const std::reference_wrapper<const SD> &), int> ();
    216     test_no_result<PMS0C(volatile       S&)>();
    217     test_no_result<PMS0C(const volatile S&)>();
    218 
    219     test_result_of<PMS1C(               S,   int), int*> ();
    220     test_result_of<PMS1C(               S&,  int), int*> ();
    221     test_result_of<PMS1C(const          S&,  int), int*> ();
    222     test_result_of<PMS1C(               S*,  int), int*> ();
    223     test_result_of<PMS1C(const          S*,  int), int*> ();
    224     test_result_of<PMS1C(               S*&, int), int*> ();
    225     test_result_of<PMS1C(const          S*&, int), int*> ();
    226     test_result_of<PMS1C(std::unique_ptr<S>, int), int*> ();
    227     test_no_result<PMS1C(volatile       S&, int)>();
    228     test_no_result<PMS1C(const volatile S&, int)>();
    229 
    230     test_result_of<PMS2C(               S,   int, int), int&> ();
    231     test_result_of<PMS2C(               S&,  int, int), int&> ();
    232     test_result_of<PMS2C(const          S&,  int, int), int&> ();
    233     test_result_of<PMS2C(               S*,  int, int), int&> ();
    234     test_result_of<PMS2C(const          S*,  int, int), int&> ();
    235     test_result_of<PMS2C(               S*&, int, int), int&> ();
    236     test_result_of<PMS2C(const          S*&, int, int), int&> ();
    237     test_result_of<PMS2C(std::unique_ptr<S>, int, int), int&> ();
    238     test_no_result<PMS2C(volatile       S&, int, int)>();
    239     test_no_result<PMS2C(const volatile S&, int, int)>();
    240 
    241     typedef int       (S::*PMS0V)() volatile;
    242     typedef int*       (S::*PMS1V)(long) volatile;
    243     typedef int&       (S::*PMS2V)(long, int) volatile;
    244     test_result_of<PMS0V(               S),   int> ();
    245     test_result_of<PMS0V(               S&),  int> ();
    246     test_result_of<PMS0V(volatile       S&),  int> ();
    247     test_result_of<PMS0V(               S*),  int> ();
    248     test_result_of<PMS0V(volatile       S*),  int> ();
    249     test_result_of<PMS0V(               S*&), int> ();
    250     test_result_of<PMS0V(volatile       S*&), int> ();
    251     test_result_of<PMS0V(std::unique_ptr<S>), int> ();
    252     test_no_result<PMS0V(const          S&)>();
    253     test_no_result<PMS0V(const volatile S&)>();
    254 
    255     test_result_of<PMS1V(               S,   int), int*> ();
    256     test_result_of<PMS1V(               S&,  int), int*> ();
    257     test_result_of<PMS1V(volatile       S&,  int), int*> ();
    258     test_result_of<PMS1V(               S*,  int), int*> ();
    259     test_result_of<PMS1V(volatile       S*,  int), int*> ();
    260     test_result_of<PMS1V(               S*&, int), int*> ();
    261     test_result_of<PMS1V(volatile       S*&, int), int*> ();
    262     test_result_of<PMS1V(std::unique_ptr<S>, int), int*> ();
    263     test_no_result<PMS1V(const          S&, int)>();
    264     test_no_result<PMS1V(const volatile S&, int)>();
    265 
    266     test_result_of<PMS2V(               S,   int, int), int&> ();
    267     test_result_of<PMS2V(               S&,  int, int), int&> ();
    268     test_result_of<PMS2V(volatile       S&,  int, int), int&> ();
    269     test_result_of<PMS2V(               S*,  int, int), int&> ();
    270     test_result_of<PMS2V(volatile       S*,  int, int), int&> ();
    271     test_result_of<PMS2V(               S*&, int, int), int&> ();
    272     test_result_of<PMS2V(volatile       S*&, int, int), int&> ();
    273     test_result_of<PMS2V(std::unique_ptr<S>, int, int), int&> ();
    274     test_no_result<PMS2V(const          S&, int, int)>();
    275     test_no_result<PMS2V(const volatile S&, int, int)>();
    276 
    277     typedef int        (S::*PMS0CV)() const volatile;
    278     typedef int*       (S::*PMS1CV)(long) const volatile;
    279     typedef int&       (S::*PMS2CV)(long, int) const volatile;
    280     test_result_of<PMS0CV(               S),   int> ();
    281     test_result_of<PMS0CV(               S&),  int> ();
    282     test_result_of<PMS0CV(const          S&),  int> ();
    283     test_result_of<PMS0CV(volatile       S&),  int> ();
    284     test_result_of<PMS0CV(const volatile S&),  int> ();
    285     test_result_of<PMS0CV(               S*),  int> ();
    286     test_result_of<PMS0CV(const          S*),  int> ();
    287     test_result_of<PMS0CV(volatile       S*),  int> ();
    288     test_result_of<PMS0CV(const volatile S*),  int> ();
    289     test_result_of<PMS0CV(               S*&), int> ();
    290     test_result_of<PMS0CV(const          S*&), int> ();
    291     test_result_of<PMS0CV(volatile       S*&), int> ();
    292     test_result_of<PMS0CV(const volatile S*&), int> ();
    293     test_result_of<PMS0CV(std::unique_ptr<S>), int> ();
    294 
    295     test_result_of<PMS1CV(               S,   int), int*> ();
    296     test_result_of<PMS1CV(               S&,  int), int*> ();
    297     test_result_of<PMS1CV(const          S&,  int), int*> ();
    298     test_result_of<PMS1CV(volatile       S&,  int), int*> ();
    299     test_result_of<PMS1CV(const volatile S&,  int), int*> ();
    300     test_result_of<PMS1CV(               S*,  int), int*> ();
    301     test_result_of<PMS1CV(const          S*,  int), int*> ();
    302     test_result_of<PMS1CV(volatile       S*,  int), int*> ();
    303     test_result_of<PMS1CV(const volatile S*,  int), int*> ();
    304     test_result_of<PMS1CV(               S*&, int), int*> ();
    305     test_result_of<PMS1CV(const          S*&, int), int*> ();
    306     test_result_of<PMS1CV(volatile       S*&, int), int*> ();
    307     test_result_of<PMS1CV(const volatile S*&, int), int*> ();
    308     test_result_of<PMS1CV(std::unique_ptr<S>, int), int*> ();
    309 
    310     test_result_of<PMS2CV(               S,   int, int), int&> ();
    311     test_result_of<PMS2CV(               S&,  int, int), int&> ();
    312     test_result_of<PMS2CV(const          S&,  int, int), int&> ();
    313     test_result_of<PMS2CV(volatile       S&,  int, int), int&> ();
    314     test_result_of<PMS2CV(const volatile S&,  int, int), int&> ();
    315     test_result_of<PMS2CV(               S*,  int, int), int&> ();
    316     test_result_of<PMS2CV(const          S*,  int, int), int&> ();
    317     test_result_of<PMS2CV(volatile       S*,  int, int), int&> ();
    318     test_result_of<PMS2CV(const volatile S*,  int, int), int&> ();
    319     test_result_of<PMS2CV(               S*&, int, int), int&> ();
    320     test_result_of<PMS2CV(const          S*&, int, int), int&> ();
    321     test_result_of<PMS2CV(volatile       S*&, int, int), int&> ();
    322     test_result_of<PMS2CV(const volatile S*&, int, int), int&> ();
    323     test_result_of<PMS2CV(std::unique_ptr<S>, int, int), int&> ();
    324     }
    325     { // pointer to member data
    326     typedef char S::*PMD;
    327     test_result_of<PMD(S&), char &>();
    328     test_result_of<PMD(S*), char &>();
    329     test_result_of<PMD(S* const), char &>();
    330     test_result_of<PMD(const S&), const char&> ();
    331     test_result_of<PMD(const S*), const char&> ();
    332     test_result_of<PMD(volatile S&), volatile char&> ();
    333     test_result_of<PMD(volatile S*), volatile char&> ();
    334     test_result_of<PMD(const volatile S&), const volatile char&> ();
    335     test_result_of<PMD(const volatile S*), const volatile char&> ();
    336     test_result_of<PMD(SD&), char &>();
    337     test_result_of<PMD(SD const&), const char&>();
    338     test_result_of<PMD(SD*), char&>();
    339     test_result_of<PMD(const SD*), const char&>();
    340     test_result_of<PMD(std::unique_ptr<S>), char &>();
    341     test_result_of<PMD(std::unique_ptr<S const>), const char&>();
    342 #if TEST_STD_VER >= 11
    343     test_result_of<PMD(std::reference_wrapper<S>), char&>();
    344     test_result_of<PMD(std::reference_wrapper<S const>), const char&>();
    345 #endif
    346     test_no_result<PMD(ND&)>();
    347     }
    348 }
    349