有人可以向我解释一下是什么type*
意思吗?
我在 std::enable_if 的文档中看到这个例子:
// #3, enabled via a parameter
template<class T>
void destroy(
T* t,
typename std::enable_if<std::is_trivially_destructible<T>::value>::type* = 0
){
std::cout << "destroying trivially destructible T\n";
}
为什么我们在这里使用 type ,什么是type*
?
谢谢!
它是一个指向由std::enable_if
if std::is_trivially_destructible<T>::value == true
else公开的类型的指针,否则它不存在。它公开的默认类型是void
.
请记住,使用 SFINAE 我们只是试图触发替换错误,我们可以通过尝试使用typedef type
of来做到这一点std::enable_if
。如果std::is_trivially_destructible<T>::value
是false
然后type
将不存在并且该函数将被跳过以进行重载解析。
我们也可以指定我们自己的类型,也许这样就清楚了:
std::enable_if<true, int>::type* intPointer;
在这里,intPointer
将是 类型int*
。
如果没有检查enable_if
它看起来有点像:
template <typename T>
struct enable_always
{
typedef T type;
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句