使用 typename = enable_if_t<...>
有什么区别吗?和 enable_if_t<...,bool> = true
对于 SFINAE?我特别问,因为我偶然发现了一个看起来像错误的东西:Compiler error with a fold expression in enable_if_t
所以我很好奇这两者之间是否有任何实际区别。
最佳答案
有细微差别,但都可用于 SFINAE。
typename = enable_if_t<...>
表单不允许“简单”重载:
template <typename T, typename = enable_if_t<cond<T>::value>>
void foo();
template <typename T, typename = enable_if_t<!cond<T>::value>>
void foo(); // Error: redeclaration of same function as default are not part of signature
// Both are just template <typename, typename> void foo()
enable_if_t<cond, bool> = true
不受此影响:
template <typename T, enable_if_t<cond<T>::value, bool> = true>
void foo();
template <typename T, enable_if_t<!cond<T>::value, bool> = true>
void foo();
typename = enable_if_t<...>
的另一个问题是使用可能被劫持:
template <typename T, typename = enable_if_t<cond<T>::value>>
void foo();
template <typename T, typename = enable_if_t<cond<T>::value>>
void bar(T);
foo<int>(); // Regular usage, SFINAE occurs
bar(42); // Regular usage, SFINAE occurs
bar<int>(42); // Possible usage, SFINAE still occurs
// But
foo<int, void>(); // No substitution fails here, so no SFINAE
bar<int, void>(42); // No substitution fails here, so no SFINAE
关于c++ - `typename = enable_if_t<...>` 和 `enable_if_t<...,bool> = true` 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56978625/