1 | //===- TypeTraitsTest.cpp - type_traits unit tests ------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "llvm/ADT/STLExtras.h" |
10 | #include "gtest/gtest.h" |
11 | |
12 | using namespace llvm; |
13 | |
14 | //===----------------------------------------------------------------------===// |
15 | // function_traits |
16 | //===----------------------------------------------------------------------===// |
17 | |
18 | namespace { |
19 | /// Check a callable type of the form `bool(const int &)`. |
20 | template <typename CallableT> struct CheckFunctionTraits { |
21 | static_assert( |
22 | std::is_same_v<typename function_traits<CallableT>::result_t, bool>, |
23 | "expected result_t to be `bool`" ); |
24 | static_assert( |
25 | std::is_same_v<typename function_traits<CallableT>::template arg_t<0>, |
26 | const int &>, |
27 | "expected arg_t<0> to be `const int &`" ); |
28 | static_assert(function_traits<CallableT>::num_args == 1, |
29 | "expected num_args to be 1" ); |
30 | }; |
31 | |
32 | /// Test function pointers. |
33 | using FuncType = bool (*)(const int &); |
34 | struct CheckFunctionPointer : CheckFunctionTraits<FuncType> {}; |
35 | |
36 | /// Test method pointers. |
37 | struct Foo { |
38 | bool func(const int &v); |
39 | }; |
40 | struct CheckMethodPointer : CheckFunctionTraits<decltype(&Foo::func)> {}; |
41 | |
42 | /// Test lambda references. |
43 | LLVM_ATTRIBUTE_UNUSED auto lambdaFunc = [](const int &v) -> bool { |
44 | return true; |
45 | }; |
46 | struct CheckLambda : CheckFunctionTraits<decltype(lambdaFunc)> {}; |
47 | |
48 | } // end anonymous namespace |
49 | |
50 | //===----------------------------------------------------------------------===// |
51 | // is_detected |
52 | //===----------------------------------------------------------------------===// |
53 | |
54 | namespace { |
55 | struct HasFooMethod { |
56 | void foo() {} |
57 | }; |
58 | struct NoFooMethod {}; |
59 | |
60 | template <class T> using has_foo_method_t = decltype(std::declval<T &>().foo()); |
61 | |
62 | static_assert(is_detected<has_foo_method_t, HasFooMethod>::value, |
63 | "expected foo method to be detected" ); |
64 | static_assert(!is_detected<has_foo_method_t, NoFooMethod>::value, |
65 | "expected no foo method to be detected" ); |
66 | } // end anonymous namespace |
67 | |