• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*<-
2 Copyright (c) 2016 Barrett Adair
3 
4 Distributed under the Boost Software License, Version 1.0.
5 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
6 ->*/
7 
8 //[ apply_member_pointer
9 #include <type_traits>
10 #include <boost/callable_traits/apply_member_pointer.hpp>
11 
12 namespace ct = boost::callable_traits;
13 
14 struct foo;
15 struct bar;
16 
main()17 int main() {
18 
19     {
20         // function type -> member function pointer type
21         using f = int(int);
22         using g = ct::apply_member_pointer_t<f, foo>;
23         using expect = int(foo::*)(int);
24         static_assert(std::is_same<g, expect>::value, "");
25     }
26 
27     {
28         // function pointer type (unqualified) -> member function pointer type
29         using f = int(*)();
30         using g = ct::apply_member_pointer_t<f, foo>;
31         using expect = int(foo::*)();
32         static_assert(std::is_same<g, expect>::value, "");
33     }
34 
35 
36     {
37         // function pointer type (qualified) -> member data pointer type
38 
39         // Look out for cases like these two. If the input type to apply_member_pointer
40         // is a ``[*qualified]`` function pointer type, then the  aliased type is a member data
41         // pointer to a ``[*qualified function pointer]``.
42 
43         {
44             using f = int(*&)();
45             using g = ct::apply_member_pointer_t<f, foo>;
46             using expect = int (* foo::*)();
47             static_assert(std::is_same<g, expect>::value, "");
48         }
49 
50         {
51             using f = int(* const)();
52             using g = ct::apply_member_pointer_t<f, foo>;
53             using expect = int (* const foo::*)();
54             static_assert(std::is_same<g, expect>::value, "");
55         }
56     }
57 
58     {
59         // function reference type -> member function pointer type
60         using f = void(&)();
61         using g = ct::apply_member_pointer_t<f, foo>;
62         using expect = void(foo::*)();
63         static_assert(std::is_same<g, expect>::value, "");
64     }
65 
66     {
67         // member function pointer type -> member function pointer type
68         // (note the different parent class)
69         using f = int(bar::*)() const;
70         using g = ct::apply_member_pointer_t<f, foo>;
71         using expect = int(foo::*)() const;
72         static_assert(std::is_same<g, expect>::value, "");
73     }
74 
75     {
76         // non-callable type -> member data pointer type
77         using g = ct::apply_member_pointer_t<int, foo>;
78         using expect = int foo::*;
79         static_assert(std::is_same<g, expect>::value, "");
80     }
81 
82 
83     {
84         // function object type -> member data pointer type
85         // the same is true for lambdas and generic lambdas
86         auto lambda = [](){};
87         using f = decltype(lambda);
88         using g = ct::apply_member_pointer_t<f, foo>;
89         using expect = f foo::*;
90         static_assert(std::is_same<g, expect>::value, "");
91     }
92 }
93 //]
94 
95