泛型编程技巧——使用std::enable_if实现按类型进行条件编译
std::enable_if
可以以多种形式使用,包括:
- 作为类模板或函数模板参数
- 作为返回类型(不适用于构造函数和析构函数)
- 作为附加函数参数(不适用于运算符重载)
std::enable_if
can be used in many forms, including:
- as an additional function argument (not applicable to most operator overloads),
- as a return type (not applicable to constructors and destructors),
- as a class template or function template parameter.
std::enable_if
核心是结合 SFINAE(Substitution Failure Is Not An Error)机制,实现编译期的条件分支逻辑。
c++14中,定义了using enable_if_t = typename enable_if<B,T>::type;
1. 作为类模板或函数模板参数
1.1 控制类模板的参数类型
例子,
// the partial specialization of A is enabled via a template parameter
template<class T, class Enable = void>
class A {}; // primary templatetemplate<class T>
class A<T, typename std::enable_if<std::is_floating_point<T>::value>::type>
{}; // specialization for floating point typesint main()
{A<int>{}; // OK: matches the primary templateA<double>{}; // OK: matches the partial specialization
}
另一个例子,
// 基础模板
template <typename Mat, typename Vec, typename Enable = void>
class Derived : public Base<Mat, Vec> {}; // 模版特化,针对double类型
template <typename Mat, typename Vec>
class Derived<Mat, Vec, std::enable_if_t<std::is_same_v<typename Mat::ElementType, std::complex<double>>>> : public Base<Mat, Vec> {};// 模版特化,针对std::complex<double>类型
template <typename Mat, typename Vec>
class Derived<Mat, Vec, std::enable_if_t<std::is_same_v<typename Mat::ElementType, std::complex<double>>>> : public Base<Mat, Vec> {};
在这个例子中,Derived继承自Base基类,但是能够针对Mat元素类型来进行特化。
这个例子使用了c++14新特性enable_if_t 。
1.2 控制函数模板的参数类型
例如,根据类型是否为整数类型来选择不同的函数实现:
#include <type_traits>
#include <iostream>template<typename T, typename std::enable_if<std::is_integral<T>::value, T>::type* = nullptr>
void print(T value) {std::cout << "Integral value: " << value << std::endl;
}template<typename T, typename std::enable_if<!std::is_integral<T>::value, T>::type* = nullptr>
void print(T value) {std::cout << "Non-integral value: " << value << std::endl;
}int main() {print(5); // Integral value: 5print(3.14); // Non-integral value: 3.14return 0;
}
在这个例子中:
-
第一个
print
函数模板仅对整数类型有效(std::is_integral<T>::value
为true
)。 -
第二个
print
函数模板对非整数类型有效(std::is_integral<T>::value
为false
)。
2. 作为返回类型
例子,
#include <type_traits>
#include <iostream>template <typename T>
typename std::enable_if<std::is_integral<T>::value, T>::type
foo(T x)
{std::cout << "foo int";return x * 2;
}template <typename T>
typename std::enable_if<std::is_floating_point<T>::value, T>::type
foo(T x)
{std::cout << "foo float";return x / 2.0;
}
在这个示例中,我们定义了两个函数模板foo
,它们根据不同的类型来返回不同的结果。第一个foo
函数只能接受整型参数,并返回这个整数的两倍。第二个foo
函数只能接受浮点型参数,并返回这个浮点数的一半。
我们运行
foo(2);
foo(2.1);
将得到打印结果
foo int
foo float
3. 作为附加函数参数
以下是一个使用 std::enable_if
作为附加函数参数的示例,假设我们希望根据传入的类型是否为指针类型来选择不同的函数实现:
#include <iostream>
#include <type_traits>// 重载函数1:仅对指针类型有效
template<typename T>
void print(T* value, typename std::enable_if<std::is_pointer<T*>::value>::type* = nullptr) {std::cout << "Pointer value: " << *value << std::endl;
}// 重载函数2:对非指针类型有效
template<typename T>
void print(T value, typename std::enable_if<!std::is_pointer<T>::value>::type* = nullptr) {std::cout << "Non-pointer value: " << value << std::endl;
}int main() {int a = 10;int* ptr = &a;print(ptr); // 调用指针版本的 printprint(a); // 调用非指针版本的 printreturn 0;
}
运行程序后,输出结果如下:
Pointer value: 10
Non-pointer value: 10
参考:
https://en.cppreference.com/w/cpp/types/enable_if.html
C++ std::enable_if的简明指南_c enable if-CSDN博客