c++ 模板实例化类型判断 & 编译期错误抛出

c++ 模板实例化【编译期】类型判断 &错误抛出

应用场景1 — 实例化【编译期】类型判断

在某个时刻,我们模板实例化时,可能需要对特别的类型做不同的处理。
栗子如下:

if constexpr (std::is_same_v<T,float>){
 	...
}
else {
	...
}

备注:“if constexpr” 为c++17特性,其他分支判断方法,见后面分支判断说明

应用场景2 — 实例化【编译期】错误抛出

也许某个时刻,我们的模板中,不支持特定类型的处理,在编译器就报错,好让程序猿的我们,不在苦苦查找bug。
栗子如下:

static_assert(std::is_arithmetic_v<T>,"type is self define type")
std::is_integral_v 		//是否为整型判断
std::is_class_v   	    //是否为类判断
std::is_function_v 		//是否为函数判断

模板分支判断方法

1、SFINAE

即匹配失败不是错误,英文Substitution Failure Is Not An Error

template<typename T>
std::enable_if_t<std::is_integral_v<T>, T> simpleTypeInfo(T t)
{
	cout << "foo " << t << '\n';
	return t;
}
 
template<typename T>
std::enable_if_t<not std::is_integral_v<T>, T> simpleTypeInfo(T t)
{
	cout << "not integral \n";
	return t;
}

2、标签分发(tag dispatching)

template <typename T>
T simpleTypeInfoTagImpl(T t, std::true_type) {
	std::cout << "foo " << t << '\n';
	return t;
}
template <typename T>
T simpleTypeInfoTagImpl(T t, std::false_type) {
	std::cout << "not integral \n";
	return t;
}
template <typename T>
T simpleTypeInfoTag(T t) {
	return simpleTypeInfoTagImpl(t, std::is_integral<T>{});
}

3、编译时if(if constexpr)

template <typename T>
T simpleTypeInfo(T t) {
	if constexpr (std::is_integral_v<T>) {
		std::cout << "foo " << t << '\n';
	}
	else {
		std::cout << "not integral \n";
	}
	return t;
}

运行时"typeid"类型判断

int a = 15;
 A str;
 const char *p = "World";
 cout << "Hello World!" << endl;
 // 直接输出类型名称
 cout << typeid(int).name() << endl;
 // 输出变量a的类型名称
 cout << typeid(a).name() << endl;
 // 输出结构体str的类型
 cout << typeid(str).name() << endl;
 // 输出计算结果的类型
 cout << typeid(1.23*3.4).name() << endl;
 // 输出字符串的类型
 cout << typeid("hello").name() << endl;
 // 输出指针类型
 cout << typeid(p).name() << endl;

参考连接

  • C++17之if constexpr
  • C++ typeid关键字详解

你可能感兴趣的:(C++,c++)