std::enable_if
来自 cppreference.cn
定义于头文件 <type_traits> |
||
template< bool B, class T = void > struct enable_if; |
(C++11 起) | |
如果 B
为 true,则 std::enable_if
有一个公共成员 typedef type
,等于 T
;否则,没有成员 typedef。
在 C++20 的概念出现之前,此元函数是利用 SFINAE 的便捷方式,特别是用于根据类型特性有条件地从候选集中移除函数,从而允许基于这些不同类型特性进行独立的函数重载或特化。
std::enable_if
可以以多种形式使用,包括:
- 作为额外的函数参数(不适用于大多数运算符重载),
- 作为返回类型(不适用于构造函数和析构函数),
- 作为类模板或函数模板参数。
如果程序为 std::enable_if
添加特化,则行为未定义。
目录 |
[编辑] 成员类型
类型 | 定义 |
类型
|
根据 B 的值,可以是 T 或没有此类成员 |
[编辑] 辅助类型
template< bool B, class T = void > using enable_if_t = typename enable_if<B,T>::type; |
(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
时应小心。一些 ABI 规范(如 Itanium ABI)不包括非类型模板参数的实例化依赖部分在名称修饰中,这意味着两个不同函数模板的特化最终可能会得到相同的修饰名并被错误地链接在一起。例如:
// 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 可变参数别名模板 (别名模板) |