[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / llvm / unittests / ADT / TypeTraitsTest.cpp
blob30fb98a3f2f54f48d793b79f99f6b0b9049dae90
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 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/STLExtras.h"
10 #include "gtest/gtest.h"
12 using namespace llvm;
14 //===----------------------------------------------------------------------===//
15 // function_traits
16 //===----------------------------------------------------------------------===//
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<typename function_traits<CallableT>::result_t, bool>::value,
23 "expected result_t to be `bool`");
24 static_assert(
25 std::is_same<typename function_traits<CallableT>::template arg_t<0>,
26 const int &>::value,
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.
37 struct Foo {
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 {
44 return true;
46 struct CheckLambda : CheckFunctionTraits<decltype(lambdaFunc)> {};
48 } // end anonymous namespace
50 //===----------------------------------------------------------------------===//
51 // is_detected
52 //===----------------------------------------------------------------------===//
54 namespace {
55 struct HasFooMethod {
56 void foo() {}
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