std::enable_if
来自 cppreference.cn
定义于头文件 <type_traits> |
||
template< bool B, class T = void > struct enable_if; |
(since C++11) | |
如果 B
为 true,则 std::enable_if
拥有一个公共成员 typedef type
,等同于 T
;否则,没有成员 typedef。
此元函数是在 C++20 的概念 (concepts) 之前利用 SFINAE 的便捷方法,特别是用于基于类型特征有条件地从候选集中移除函数,从而允许基于这些不同类型特征的单独函数重载或特化。
std::enable_if
可以以多种形式使用,包括
- 作为额外的函数参数(不适用于大多数运算符重载),
- 作为返回类型(不适用于构造函数和析构函数),
- 作为类模板或函数模板参数。
如果程序为 std::enable_if
添加特化,则行为未定义。
目录 |
[编辑] 成员类型
类型 | 定义 |
type
|
T 或没有此成员,取决于 B 的值 |
[编辑] 辅助类型
template< bool B, class T = void > using enable_if_t = typename enable_if<B,T>::type; |
(since C++14) | |
[编辑] 可能的实现
template<bool B, class T = void> struct enable_if {}; template<class T> struct enable_if<true, T> { typedef T type; }; |
[编辑] 注意事项
常见的错误是声明两个仅默认模板参数不同的函数模板。这不起作用,因为这些声明被视为同一函数模板的重新声明(默认模板参数在函数模板等价性中不被考虑)。
/* WRONG */ struct T { enum { int_t, float_t } type; template<typename Integer, typename = std::enable_if_t<std::is_integral<Integer>::value>> T(Integer) : type(int_t) {} template<typename Floating, typename = std::enable_if_t<std::is_floating_point<Floating>::value>> T(Floating) : type(float_t) {} // error: treated as redefinition }; /* RIGHT */ struct T { enum { int_t, float_t } type; template<typename Integer, std::enable_if_t<std::is_integral<Integer>::value, bool> = true> T(Integer) : type(int_t) {} template<typename Floating, std::enable_if_t<std::is_floating_point<Floating>::value, bool> = true> T(Floating) : type(float_t) {} // OK };
在命名空间作用域函数模板的模板非类型参数的类型中使用 enable_if
时应谨慎。 像 Itanium ABI 这样的某些 ABI 规范在名称修饰 (mangling) 中不包括非类型模板参数的实例化相关部分,这意味着两个不同函数模板的特化最终可能会具有相同的修饰名称并被错误地链接在一起。 例如
// first translation unit struct X { enum { value1 = true, value2 = true }; }; template<class T, std::enable_if_t<T::value1, int> = 0> void func() {} // #1 template void func<X>(); // #2 // second translation unit struct X { enum { value1 = true, value2 = true }; }; template<class T, std::enable_if_t<T::value2, int> = 0> void func() {} // #3 template void func<X>(); // #4
函数模板 #1 和 #3 具有不同的签名,并且是不同的模板。 然而,#2 和 #4,尽管是不同函数模板的实例化,但在 Itanium C++ ABI 中具有相同的修饰名称 (_Z4funcI1XLi0EEvv),这意味着链接器会错误地认为它们是同一实体。
[编辑] 示例
运行此代码
#include <iostream> #include <new> #include <string> #include <type_traits> namespace detail { void* voidify(const volatile void* ptr) noexcept { return const_cast<void*>(ptr); } } // #1, enabled via the return type template<class T> typename std::enable_if<std::is_trivially_default_constructible<T>::value>::type construct(T*) { std::cout << "default constructing trivially default constructible T\n"; } // same as above template<class T> typename std::enable_if<!std::is_trivially_default_constructible<T>::value>::type construct(T* p) { std::cout << "default constructing non-trivially default constructible T\n"; ::new(detail::voidify(p)) T; } // #2 template<class T, class... Args> std::enable_if_t<std::is_constructible<T, Args&&...>::value> // Using helper type construct(T* p, Args&&... args) { std::cout << "constructing T with operation\n"; ::new(detail::voidify(p)) T(static_cast<Args&&>(args)...); } // #3, enabled via a parameter template<class T> void destroy( T*, typename std::enable_if< std::is_trivially_destructible<T>::value >::type* = 0) { std::cout << "destroying trivially destructible T\n"; } // #4, enabled via a non-type template parameter template<class T, typename std::enable_if< !std::is_trivially_destructible<T>{} && (std::is_class<T>{} || std::is_union<T>{}), bool>::type = true> void destroy(T* t) { std::cout << "destroying non-trivially destructible T\n"; t->~T(); } // #5, enabled via a type template parameter template<class T, typename = std::enable_if_t<std::is_array<T>::value>> void destroy(T* t) // note: function signature is unmodified { for (std::size_t i = 0; i < std::extent<T>::value; ++i) destroy((*t)[i]); } /* template<class T, typename = std::enable_if_t<std::is_void<T>::value>> void destroy(T* t) {} // error: has the same signature with #5 */ // the partial specialization of A is enabled via a template parameter template<class T, class Enable = void> class A {}; // primary template template<class T> class A<T, typename std::enable_if<std::is_floating_point<T>::value>::type> {}; // specialization for floating point types int main() { union { int i; char s[sizeof(std::string)]; } u; construct(reinterpret_cast<int*>(&u)); destroy(reinterpret_cast<int*>(&u)); construct(reinterpret_cast<std::string*>(&u), "Hello"); destroy(reinterpret_cast<std::string*>(&u)); A<int>{}; // OK: matches the primary template A<double>{}; // OK: matches the partial specialization }
输出
default constructing trivially default constructible T destroying trivially destructible T constructing T with operation destroying non-trivially destructible T
[编辑] 参见
(C++17) |
void 可变参数别名模板 (别名模板) |