std:: is_function
|
定义于头文件
<type_traits>
|
||
|
template
<
class
T
>
struct is_function ; |
(C++11 起) | |
std::is_function
是一个
UnaryTypeTrait
。
检查
T
是否为函数类型。类似
std::
function
、lambda 表达式、重载了
operator()
的类以及函数指针等类型均不计入函数类型。若
T
是函数类型,则提供等于
true
的成员常量
value
;否则
value
等于
false
。
如果程序为
std::is_function
或
std::is_function_v
添加特化,则行为是未定义的。
目录 |
模板参数
| T | - | 待检查的类型 |
辅助变量模板
|
template
<
class
T
>
constexpr bool is_function_v = is_function < T > :: value ; |
(C++17 起) | |
继承自 std:: integral_constant
成员常量
|
value
[static]
|
若
T
为函数类型则为
true
,否则为
false
(公开静态成员常量) |
成员函数
|
operator bool
|
转换对象为
bool
类型,返回
value
(公开成员函数) |
|
operator()
(C++14)
|
返回
value
(公开成员函数) |
成员类型
| 类型 | 定义 |
value_type
|
bool |
type
|
std:: integral_constant < bool , value > |
注释
std::is_function
可以通过更简洁的方式实现。新版本的
libc++
、
libstdc++
和
MS STL
均采用了类似以下示例的实现方式:
template<class T> struct is_function : std::integral_constant< bool, !std::is_const<const T>::value && !std::is_reference<T>::value > {};
以下实现主要用于教学目的,因为它展示了多种函数类型。
可能的实现
// 主模板 template<class> struct is_function : std::false_type {}; // 常规函数的特化 template<class Ret, class... Args> struct is_function<Ret(Args...)> : std::true_type {}; // 可变参数函数的特化(如 std::printf) template<class Ret, class... Args> struct is_function<Ret(Args......)> : std::true_type {}; // 带有 cv 限定符的函数类型特化 template<class Ret, class... Args> struct is_function<Ret(Args...) const> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) volatile> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) const volatile> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) const> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) volatile> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) const volatile> : std::true_type {}; // 带有引用限定符的函数类型特化 template<class Ret, class... Args> struct is_function<Ret(Args...) &> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) const &> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) volatile &> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) const volatile &> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) &> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) const &> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) volatile &> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) const volatile &> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) &&> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) const &&> : std::true_type {}; template<class Ret, class... Args> <span |
示例
#include <functional> #include <type_traits> int f(); static_assert(std::is_function_v<decltype(f)>); static_assert(std::is_function_v<int(int)>); static_assert(!std::is_function_v<int>); static_assert(!std::is_function_v<decltype([]{})>); static_assert(!std::is_function_v<std::function<void()>>); struct O { void operator()() {} }; static_assert(std::is_function_v<O()>); struct A { static int foo(); int fun() const&; }; static_assert(!std::is_function_v<A>); static_assert(std::is_function_v<decltype(A::foo)>); static_assert(!std::is_function_v<decltype(&A::fun)>); template<typename> struct PM_traits {}; template<class T, class U> struct PM_traits<U T::*> { using member_type = U; }; int main() { using T = PM_traits<decltype(&A::fun)>::member_type; // T 的类型是 int() const& static_assert(std::is_function_v<T>); }
参见
|
检查类型是否能用给定参数类型调用(如同通过
std::invoke
)
(类模板) |
|
|
(C++11)
|
检查类型是否为对象类型
(类模板) |
|
(C++11)
|
检查类型是否为非联合类类型
(类模板) |