C++ 的 类型特性
等级 5512 次按键
<type_traits> 中的类型特征在编译期回答关于类型的问题。analyze<T>() 针对 int、const double 和 int* 输出其中五个;未使用 boolalpha,true 和 false 输出为 1 和 0。
C++ 代码
// T 在调用时指定,例如 analyze<int>()1template<typename T>2void analyze() {3 cout << "Type info:" << endl; // 每个 _v 特征都是在编译期计算的 bool 常量4 cout << " is_integral: " << is_integral_v<T> << endl;5 cout << " is_floating_point: " << is_floating_point_v<T> << endl;6 cout << " is_pointer: " << is_pointer_v<T> << endl; // is_const 检查 T 的顶层 const7 cout << " is_const: " << is_const_v<T> << endl;8 cout << " is_reference: " << is_reference_v<T> << endl;9}10 11cout << "=== int ===" << endl;// int:只有 is_integral 为 112analyze<int>();13cout << "=== const double ===" << endl;// const double:is_floating_point 和 is_const 为 114analyze<const double>();15cout << "=== int* ===" << endl;// int*:只有 is_pointer 为 1(指针本身不是 const)16analyze<int*>();运行结果
=== int === Type info: is_integral: 1 is_floating_point: 0 is_pointer: 0 is_const: 0 is_reference: 0 === const double === Type info: is_integral: 0 is_floating_point: 1 is_pointer: 0 is_const: 1 is_reference: 0 === int* === Type info: is_integral: 0 is_floating_point: 0 is_pointer: 1 is_const: 0 is_reference: 0
可以用这段代码练习打字。成绩不会计入排行榜。
这段代码中的符号
这段 C++ 代码中需要输入的符号及其出现次数。
<× 55"× 18=× 18_× 17;× 12>× 9:× 6(× 4)× 4*× 2