当前位置:网站首页>Judge whether it is void type

Judge whether it is void type

2022-07-23 09:28:00 Hair like snow ty

c++ There is a class template in the standard library is_void , Used to judge whether a certain type is void type . stay main() Do some tests in the function :

int main()
{
    
	cout << std::is_void<int>::value << endl;
	cout << std::is_void<void>::value << endl;
	system("pause");
	return 0;
}

result :
 Insert picture description here
that is_void How to achieve it ? Actually ,is_void It can be regarded as a value extraction template .

template<typename T>
struct TraitsVoid 
{
    
	static const int value = 0;
};

template<>
struct TraitsVoid<void>
{
    
	static const int value = 1;
};

int main()
{
    
	cout << TraitsVoid<int>::value << endl;
	cout << TraitsVoid<void>::value << endl;
	system("pause");
	return 0;
}

result :
 Insert picture description here

原网站

版权声明
本文为[Hair like snow ty]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207230133255553.html