[c++] 带类型判断的模板

#include 
#include 

template
T* PrintType(int* x)
{
	if(std::is_same::value) {
		std::cout << "int type" << std::endl;
		return reinterpret_cast(x);
	}
	else {
		std::cout << "not int type" << std::endl;
		return nullptr;
	}
}

template
const T* PrintType(const int* x)
{
	if(std::is_same::value) {
		std::cout << "const int type" << std::endl;
		return reinterpret_cast< const T*>(x);
	}
	else {
		std::cout << "not const int type" << std::endl;
		return nullptr;
	}
}


int main()
{
	int x=5;
	const int y=6;
	PrintType(&x);
	PrintType(&x);
	PrintType(&y);
	PrintType(&y);
	return 0;
}

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