1c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant//===----------------------------------------------------------------------===//
2c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant//
3c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant//                     The LLVM Compiler Infrastructure
4c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant//
5b64f8b07c104c6cc986570ac8ee0ed16a9f23976Howard Hinnant// This file is dual licensed under the MIT and the University of Illinois Open
6b64f8b07c104c6cc986570ac8ee0ed16a9f23976Howard Hinnant// Source Licenses. See LICENSE.TXT for details.
7c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant//
8c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant//===----------------------------------------------------------------------===//
9c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant
10c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant// type_traits
11c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant
12c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant// is_convertible
13c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant
14c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant#include <type_traits>
15c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant
16933afa9761c1c1f916161278a99284d50a594939Marshall Clowtemplate <class T, class U>
17933afa9761c1c1f916161278a99284d50a594939Marshall Clowvoid test_is_convertible()
18933afa9761c1c1f916161278a99284d50a594939Marshall Clow{
19933afa9761c1c1f916161278a99284d50a594939Marshall Clow    static_assert((std::is_convertible<T, U>::value), "");
20933afa9761c1c1f916161278a99284d50a594939Marshall Clow    static_assert((std::is_convertible<const T, U>::value), "");
21933afa9761c1c1f916161278a99284d50a594939Marshall Clow    static_assert((std::is_convertible<T, const U>::value), "");
22933afa9761c1c1f916161278a99284d50a594939Marshall Clow    static_assert((std::is_convertible<const T, const U>::value), "");
23933afa9761c1c1f916161278a99284d50a594939Marshall Clow}
24933afa9761c1c1f916161278a99284d50a594939Marshall Clow
25933afa9761c1c1f916161278a99284d50a594939Marshall Clowtemplate <class T, class U>
26933afa9761c1c1f916161278a99284d50a594939Marshall Clowvoid test_is_not_convertible()
27933afa9761c1c1f916161278a99284d50a594939Marshall Clow{
28933afa9761c1c1f916161278a99284d50a594939Marshall Clow    static_assert((!std::is_convertible<T, U>::value), "");
29933afa9761c1c1f916161278a99284d50a594939Marshall Clow    static_assert((!std::is_convertible<const T, U>::value), "");
30933afa9761c1c1f916161278a99284d50a594939Marshall Clow    static_assert((!std::is_convertible<T, const U>::value), "");
31933afa9761c1c1f916161278a99284d50a594939Marshall Clow    static_assert((!std::is_convertible<const T, const U>::value), "");
32933afa9761c1c1f916161278a99284d50a594939Marshall Clow}
33933afa9761c1c1f916161278a99284d50a594939Marshall Clow
34c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnanttypedef void Function();
35c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnanttypedef char Array[1];
36c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant
37f9e1c7e3674284efe4fea3be799e83d72aa92dfeDouglas Gregorclass NonCopyable {
38f9e1c7e3674284efe4fea3be799e83d72aa92dfeDouglas Gregor  NonCopyable(NonCopyable&);
39f9e1c7e3674284efe4fea3be799e83d72aa92dfeDouglas Gregor};
40f9e1c7e3674284efe4fea3be799e83d72aa92dfeDouglas Gregor
41c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnantint main()
42c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant{
43933afa9761c1c1f916161278a99284d50a594939Marshall Clow    // void
44933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_convertible<void,void> ();
45933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<void,Function> ();
46933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<void,Function&> ();
47933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<void,Function*> ();
48933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<void,Array> ();
49933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<void,Array&> ();
50933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<void,char> ();
51933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<void,char&> ();
52933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<void,char*> ();
53933afa9761c1c1f916161278a99284d50a594939Marshall Clow
54933afa9761c1c1f916161278a99284d50a594939Marshall Clow    // Function
55933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Function, void> ();
56933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Function, Function> ();
57933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_convertible<Function, Function&> ();
58933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_convertible<Function, Function*> ();
59933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Function, Array> ();
60933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Function, Array&> ();
61933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Function, char> ();
62933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Function, char&> ();
63933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Function, char*> ();
64933afa9761c1c1f916161278a99284d50a594939Marshall Clow
65933afa9761c1c1f916161278a99284d50a594939Marshall Clow    // Function&
66933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Function&, void> ();
67933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Function&, Function> ();
68933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_convertible<Function&, Function&> ();
69933afa9761c1c1f916161278a99284d50a594939Marshall Clow
70933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_convertible<Function&, Function*> ();
71933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Function&, Array> ();
72933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Function&, Array&> ();
73933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Function&, char> ();
74933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Function&, char&> ();
75933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Function&, char*> ();
76933afa9761c1c1f916161278a99284d50a594939Marshall Clow
77933afa9761c1c1f916161278a99284d50a594939Marshall Clow    // Function*
78933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Function*, void> ();
79933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Function*, Function> ();
80933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Function*, Function&> ();
81933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_convertible<Function*, Function*> ();
82933afa9761c1c1f916161278a99284d50a594939Marshall Clow
83933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Function*, Array> ();
84933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Function*, Array&> ();
85933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Function*, char> ();
86933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Function*, char&> ();
87933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Function*, char*> ();
88933afa9761c1c1f916161278a99284d50a594939Marshall Clow
89933afa9761c1c1f916161278a99284d50a594939Marshall Clow    // Array
90933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Array, void> ();
91933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Array, Function> ();
92933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Array, Function&> ();
93933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Array, Function*> ();
94933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Array, Array> ();
95c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant
96c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant    static_assert((!std::is_convertible<Array, Array&>::value), "");
97c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant    static_assert(( std::is_convertible<Array, const Array&>::value), "");
98c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant    static_assert((!std::is_convertible<const Array, Array&>::value), "");
9980f918072974dbe4dc7d2fdb5bbc2cab96ef0701Howard Hinnant    static_assert(( std::is_convertible<const Array, const Array&>::value), "");
100c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant
101933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Array, char> ();
102933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Array, char&> ();
103c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant
104c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant    static_assert(( std::is_convertible<Array, char*>::value), "");
105c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant    static_assert(( std::is_convertible<Array, const char*>::value), "");
106c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant    static_assert((!std::is_convertible<const Array, char*>::value), "");
107c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant    static_assert(( std::is_convertible<const Array, const char*>::value), "");
108c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant
109933afa9761c1c1f916161278a99284d50a594939Marshall Clow    // Array&
110933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Array&, void> ();
111933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Array&, Function> ();
112933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Array&, Function&> ();
113933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Array&, Function*> ();
114933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Array&, Array> ();
115c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant
116c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant    static_assert(( std::is_convertible<Array&, Array&>::value), "");
117c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant    static_assert(( std::is_convertible<Array&, const Array&>::value), "");
118c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant    static_assert((!std::is_convertible<const Array&, Array&>::value), "");
119c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant    static_assert(( std::is_convertible<const Array&, const Array&>::value), "");
120c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant
121933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Array&, char> ();
122933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<Array&, char&> ();
123c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant
124c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant    static_assert(( std::is_convertible<Array&, char*>::value), "");
125c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant    static_assert(( std::is_convertible<Array&, const char*>::value), "");
126c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant    static_assert((!std::is_convertible<const Array&, char*>::value), "");
127c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant    static_assert(( std::is_convertible<const Array&, const char*>::value), "");
128c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant
129933afa9761c1c1f916161278a99284d50a594939Marshall Clow    // char
130933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<char, void> ();
131933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<char, Function> ();
132933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<char, Function&> ();
133933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<char, Function*> ();
134933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<char, Array> ();
135933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<char, Array&> ();
136c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant
137171771a9f5fd9e5dbbc6d6a2d9dfb0d8532b5155Howard Hinnant    test_is_convertible<char, char> ();
138171771a9f5fd9e5dbbc6d6a2d9dfb0d8532b5155Howard Hinnant
139c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant    static_assert((!std::is_convertible<char, char&>::value), "");
140c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant    static_assert(( std::is_convertible<char, const char&>::value), "");
141c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant    static_assert((!std::is_convertible<const char, char&>::value), "");
142c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant    static_assert(( std::is_convertible<const char, const char&>::value), "");
143c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant
144933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<char, char*> ();
145c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant
146933afa9761c1c1f916161278a99284d50a594939Marshall Clow    // char&
147933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<char&, void> ();
148933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<char&, Function> ();
149933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<char&, Function&> ();
150933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<char&, Function*> ();
151933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<char&, Array> ();
152933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<char&, Array&> ();
153c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant
154171771a9f5fd9e5dbbc6d6a2d9dfb0d8532b5155Howard Hinnant    test_is_convertible<char&, char> ();
155171771a9f5fd9e5dbbc6d6a2d9dfb0d8532b5155Howard Hinnant
156c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant    static_assert(( std::is_convertible<char&, char&>::value), "");
157c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant    static_assert(( std::is_convertible<char&, const char&>::value), "");
158c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant    static_assert((!std::is_convertible<const char&, char&>::value), "");
159c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant    static_assert(( std::is_convertible<const char&, const char&>::value), "");
160c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant
161933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<char&, char*> ();
162c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant
163933afa9761c1c1f916161278a99284d50a594939Marshall Clow    // char*
164933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<char*, void> ();
165933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<char*, Function> ();
166933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<char*, Function&> ();
167933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<char*, Function*> ();
168933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<char*, Array> ();
169933afa9761c1c1f916161278a99284d50a594939Marshall Clow    test_is_not_convertible<char*, Array&> ();
170c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant
171171771a9f5fd9e5dbbc6d6a2d9dfb0d8532b5155Howard Hinnant    test_is_not_convertible<char*, char> ();
172171771a9f5fd9e5dbbc6d6a2d9dfb0d8532b5155Howard Hinnant    test_is_not_convertible<char*, char&> ();
173171771a9f5fd9e5dbbc6d6a2d9dfb0d8532b5155Howard Hinnant
174c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant    static_assert(( std::is_convertible<char*, char*>::value), "");
175c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant    static_assert(( std::is_convertible<char*, const char*>::value), "");
176c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant    static_assert((!std::is_convertible<const char*, char*>::value), "");
177c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant    static_assert(( std::is_convertible<const char*, const char*>::value), "");
178933afa9761c1c1f916161278a99284d50a594939Marshall Clow
179933afa9761c1c1f916161278a99284d50a594939Marshall Clow    // NonCopyable
180f9e1c7e3674284efe4fea3be799e83d72aa92dfeDouglas Gregor    static_assert((std::is_convertible<NonCopyable&, NonCopyable&>::value), "");
181f9e1c7e3674284efe4fea3be799e83d72aa92dfeDouglas Gregor    static_assert((std::is_convertible<NonCopyable&, const NonCopyable&>::value), "");
182f9e1c7e3674284efe4fea3be799e83d72aa92dfeDouglas Gregor    static_assert((std::is_convertible<NonCopyable&, const volatile NonCopyable&>::value), "");
183f9e1c7e3674284efe4fea3be799e83d72aa92dfeDouglas Gregor    static_assert((std::is_convertible<NonCopyable&, volatile NonCopyable&>::value), "");
184f9e1c7e3674284efe4fea3be799e83d72aa92dfeDouglas Gregor    static_assert((std::is_convertible<const NonCopyable&, const NonCopyable&>::value), "");
185f9e1c7e3674284efe4fea3be799e83d72aa92dfeDouglas Gregor    static_assert((std::is_convertible<const NonCopyable&, const volatile NonCopyable&>::value), "");
186f9e1c7e3674284efe4fea3be799e83d72aa92dfeDouglas Gregor    static_assert((std::is_convertible<volatile NonCopyable&, const volatile NonCopyable&>::value), "");
187f9e1c7e3674284efe4fea3be799e83d72aa92dfeDouglas Gregor    static_assert((std::is_convertible<const volatile NonCopyable&, const volatile NonCopyable&>::value), "");
188f9e1c7e3674284efe4fea3be799e83d72aa92dfeDouglas Gregor    static_assert((!std::is_convertible<const NonCopyable&, NonCopyable&>::value), "");
189c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant}
190