1 //===- TypeTraitsTest.cpp - type_traits unit tests ------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/STLExtras.h"
10 #include "gtest/gtest.h"
14 //===----------------------------------------------------------------------===//
16 //===----------------------------------------------------------------------===//
19 /// Check a callable type of the form `bool(const int &)`.
20 template <typename CallableT
> struct CheckFunctionTraits
{
22 std::is_same
<typename function_traits
<CallableT
>::result_t
, bool>::value
,
23 "expected result_t to be `bool`");
25 std::is_same
<typename function_traits
<CallableT
>::template arg_t
<0>,
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");
32 /// Test function pointers.
33 using FuncType
= bool (*)(const int &);
34 struct CheckFunctionPointer
: CheckFunctionTraits
<FuncType
> {};
36 /// Test method pointers.
38 bool func(const int &v
);
40 struct CheckMethodPointer
: CheckFunctionTraits
<decltype(&Foo::func
)> {};
42 /// Test lambda references.
43 LLVM_ATTRIBUTE_UNUSED
auto lambdaFunc
= [](const int &v
) -> bool {
46 struct CheckLambda
: CheckFunctionTraits
<decltype(lambdaFunc
)> {};
48 } // end anonymous namespace
50 //===----------------------------------------------------------------------===//
52 //===----------------------------------------------------------------------===//
58 struct NoFooMethod
{};
60 template <class T
> using has_foo_method_t
= decltype(std::declval
<T
&>().foo());
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