65 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "llvm/ADT/STLExtras.h"10#include "gtest/gtest.h"11 12using namespace llvm;13 14//===----------------------------------------------------------------------===//15// function_traits16//===----------------------------------------------------------------------===//17 18namespace {19/// Check a callable type of the form `bool(const int &)`.20template <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.33using FuncType = bool (*)(const int &);34struct CheckFunctionPointer : CheckFunctionTraits<FuncType> {};35 36/// Test method pointers.37struct Foo {38 bool func(const int &v);39};40struct CheckMethodPointer : CheckFunctionTraits<decltype(&Foo::func)> {};41 42/// Test lambda references.43[[maybe_unused]] auto lambdaFunc = [](const int &v) -> bool { return true; };44struct CheckLambda : CheckFunctionTraits<decltype(lambdaFunc)> {};45 46} // end anonymous namespace47 48//===----------------------------------------------------------------------===//49// is_detected50//===----------------------------------------------------------------------===//51 52namespace {53struct HasFooMethod {54 void foo() {}55};56struct NoFooMethod {};57 58template <class T> using has_foo_method_t = decltype(std::declval<T &>().foo());59 60static_assert(is_detected<has_foo_method_t, HasFooMethod>::value,61 "expected foo method to be detected");62static_assert(!is_detected<has_foo_method_t, NoFooMethod>::value,63 "expected no foo method to be detected");64} // end anonymous namespace65