目录
2. CRTP (Curiously Recurring Template Pattern)
1. 限制模板函数的模板参数类型
#include <iostream>
#include <type_traits>
// Expected class type
class MyClass {};
// Function template using std::enable_if to check if the type is MyClass
template<typename T>
typename std::enable_if<std::is_same<T, MyClass>::value, void>::type
checkType() {
std::cout << "Type is MyClass" << std::endl;
}
int main() {
checkType<int>(); // Won't compile, int is not MyClass
checkType<MyClass>(); // Will compile and print "Type is MyClass"
return 0;
}