111 lines · cpp
1//===----------------------------------------------------------------------===//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// type_traits10 11// is_function12 13#include <type_traits>14#include <cstddef> // for std::nullptr_t15 16#include "test_macros.h"17 18// NOTE: On Windows the function `test_is_function<void()>` and19// `test_is_function<void() noexcept> has the same mangled despite being20// a distinct instantiation. This causes Clang to emit an error. However21// structs do not have this problem.22 23template <class T>24struct test_is_function {25 static_assert( std::is_function<T>::value, "");26 static_assert( std::is_function<const T>::value, "");27 static_assert( std::is_function<volatile T>::value, "");28 static_assert( std::is_function<const volatile T>::value, "");29#if TEST_STD_VER > 1430 static_assert( std::is_function_v<T>, "");31 static_assert( std::is_function_v<const T>, "");32 static_assert( std::is_function_v<volatile T>, "");33 static_assert( std::is_function_v<const volatile T>, "");34#endif35};36 37template <class T>38struct test_is_not_function {39 static_assert(!std::is_function<T>::value, "");40 static_assert(!std::is_function<const T>::value, "");41 static_assert(!std::is_function<volatile T>::value, "");42 static_assert(!std::is_function<const volatile T>::value, "");43#if TEST_STD_VER > 1444 static_assert(!std::is_function_v<T>, "");45 static_assert(!std::is_function_v<const T>, "");46 static_assert(!std::is_function_v<volatile T>, "");47 static_assert(!std::is_function_v<const volatile T>, "");48#endif49};50 51class Empty52{53};54 55class NotEmpty56{57 virtual ~NotEmpty();58};59 60union Union {};61 62struct bit_zero63{64 int : 0;65};66 67class Abstract68{69 virtual ~Abstract() = 0;70};71 72enum Enum {zero, one};73struct incomplete_type;74 75typedef void (*FunctionPtr)();76 77int main(int, char**)78{79 test_is_function<void(void)>();80 test_is_function<int(int)>();81 test_is_function<int(int, double)>();82 test_is_function<int(Abstract *)>();83 test_is_function<void(...)>();84 85 test_is_not_function<std::nullptr_t>();86 test_is_not_function<void>();87 test_is_not_function<int>();88 test_is_not_function<int&>();89 test_is_not_function<int&&>();90 test_is_not_function<int*>();91 test_is_not_function<double>();92 test_is_not_function<char[3]>();93 test_is_not_function<char[]>();94 test_is_not_function<Union>();95 test_is_not_function<Enum>();96 test_is_not_function<FunctionPtr>(); // function pointer is not a function97 test_is_not_function<Empty>();98 test_is_not_function<bit_zero>();99 test_is_not_function<NotEmpty>();100 test_is_not_function<Abstract>();101 test_is_not_function<Abstract*>();102 test_is_not_function<incomplete_type>();103 104#if TEST_STD_VER >= 11105 test_is_function<void() noexcept>();106 test_is_function<void() const && noexcept>();107#endif108 109 return 0;110}111