enable_if和if constexpr

#include<iostream>
using namespace std;
void func() {
    
    
	cout << "test" << endl;
}
class Base {
    
    
public:int x;
};
class Derive :public Base{
    
    
public:int y;
};
template<bool,typename T>
struct my_enable_if {
    
    };
template<typename T>
struct my_enable_if<true,T> {
    
    
	using type = T;
};
template<typename T>
struct my_is_void {
    
    
};
template<>
struct my_is_void<void> {
    
    
	static constexpr bool value = true;
}; 
int main() {
    
    

	my_enable_if<my_is_void<decltype(func())>::value, int> x;
	if constexpr (my_is_void<decltype(func())>::value) {
    
    
		cout << "void " << endl;
	}
	else {
    
    
		cout << "not void" << endl;
	}
	if constexpr (is_base_of<Base, Derive>::value) {
    
    
		cout << "base test" << endl;
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_39057744/article/details/120791427