std:: is_scalar
From cppreference.net
C++
Metaprogramming library
| Type traits | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compile-time rational arithmetic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compile-time integer sequences | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
(C++14)
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<type_traits>
|
||
|
template
<
class
T
>
struct is_scalar ; |
(C++11 起) | |
std::is_scalar
是一个
UnaryTypeTrait
。
如果
T
是
标量类型
,则提供等于
true
的成员常量
value
。对于任何其他类型,
value
为
false
。
如果程序为
std::is_scalar
或
std::is_scalar_v
添加特化,则行为是未定义的。
目录 |
模板参数
| T | - | 待检查的类型 |
辅助变量模板
|
template
<
class
T
>
constexpr bool is_scalar_v = is_scalar < 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 > |
注释
C++内存模型中的每个独立内存位置(包括语言特性使用的隐藏内存位置,如虚函数表指针)都具有标量类型(或是长度非零的相邻位字段序列)。表达式求值中的副作用排序、线程间同步以及依赖顺序都是针对独立标量对象进行定义的。
可能的实现
template<class T> struct is_scalar : std::integral_constant<bool, std::is_arithmetic<T>::value || std::is_enum<T>::value || std::is_pointer<T>::value || std::is_member_pointer<T>::value || std::is_null_pointer<T>::value> {}; |
示例
运行此代码
#include <iostream> #include <type_traits> #include <typeinfo> #include <utility> template<typename Head, typename... Tail> void are_scalars(Head&& head, Tail&&... tail) { using T = std::decay_t<decltype(head)>; std::cout << typeid(T).name() << " is " << (std::is_scalar_v<T> ? "" : "not ") << "a scalar\n"; if constexpr (sizeof... (Tail)) { are_scalars(std::forward<decltype(tail)>(tail)...); } } int main() { struct S { int m; } s; int S::* mp = &S::m; enum class E { e }; are_scalars(42, 3.14, E::e, "str", mp, nullptr, s); }
可能的输出:
int is a scalar double is a scalar main::E is a scalar char const* is a scalar int main::S::* is a scalar nullptr is a scalar main::S is not a scalar
参见
|
(C++11)
|
检查类型是否为算术类型
(类模板) |
|
(C++11)
|
检查类型是否为枚举类型
(类模板) |
|
(C++11)
|
检查类型是否为指针类型
(类模板) |
|
(C++11)
|
检查类型是否为指向非静态成员函数或对象的指针
(类模板) |