广州市公司网站建设,太平洋在线企业网站管理系统,河北建设工程信息网官网官网官,wordpress ajax 主题类型特性
类型特性定义一个编译时基于模板的结构#xff0c;以查询或修改类型的属性。
试图特化定义于 type_traits 头文件的模板导致未定义行为#xff0c;除了 std::common_type 可依照其所描述特化。
定义于type_traits头文件的模板可以用不完整类型实…类型特性
类型特性定义一个编译时基于模板的结构以查询或修改类型的属性。
试图特化定义于 type_traits 头文件的模板导致未定义行为除了 std::common_type 可依照其所描述特化。
定义于type_traits头文件的模板可以用不完整类型实例化除非另外有指定尽管通常禁止以不完整类型实例化标准库模板。
杂项变换
基于编译时布尔值选择一个类型或另一个
std::conditional template bool B, class T, class F struct conditional; (C11 起)
提供成员 typedef type 若 B 在编译时为 true 则定义为 T 或若 B 为 false 则定义为 F 。
成员类型
成员类型定义type若 B true 则为 T 若 B false 则为 F
辅助类型 template bool B, class T, class F using conditional_t typename conditionalB,T,F::type; (C14 起)
可能的实现
templatebool B, class T, class F
struct conditional { typedef T type; };templateclass T, class F
struct conditionalfalse, T, F { typedef F type; };
调用示例
#include iostream
#include type_traits
#include typeinfostruct A
{
};struct B
{
};int main()
{typedef std::conditionaltrue, int, double::type Type1;typedef std::conditionalfalse, int, double::type Type2;typedef std::conditionalsizeof(int) sizeof(double), int, double ::type Type3;typedef std::conditionalfalse, A, B::type Type4;typedef std::conditionaltrue, std::string, char*::type Type5;std::cout std::conditionaltrue, int, double::type: typeid(Type1).name() std::endl;std::cout std::conditionalfalse, int, double::type: typeid(Type2).name() std::endl;std::cout std::conditionalsizeof(int)sizeof(double),int,double::type: typeid(Type3).name() std::endl;std::cout std::conditionalfalse, A, B::type: typeid(Type4).name() std::endl;std::cout std::conditionaltrue, std::string, char*::type: typeid(Type5).name() std::endl;return 0;
}输出
std::conditionaltrue, int, double::type: i
std::conditionalfalse, int, double::type: d
std::conditionalsizeof(int)sizeof(double),int,double::type:d
std::conditionalfalse, A, B::type: 1B
std::conditionaltrue, std::string, char*::type: NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE