为什么 std::void_t 在这种情况下不起作用?
Why does std::void_t not work in such a case?
#include <type_traits>
template<typename, typename = void>
struct IsIterator final : std::false_type
{};
template<typename T>
struct IsIterator<T,
std::void_t<std::enable_if_t<std::is_base_of_v<std::input_iterator_tag,
typename std::iterator_traits<T>::iterator_category>>>>
final : std::true_type
{};
int main()
{
return IsIterator<void*>::value;
}
clang 8.0 给出以下错误信息:
/usr/bin/../include/c++/v1/iterator:507:16: error: cannot form a reference to 'void'
typedef _Tp& reference;
^
main.cpp:20:23: note: in instantiation of template class 'std::__1::iterator_traits<void *>' requested
here
typename std::iterator_traits<T>::iterator_category>>>>
^
main.cpp:29:16: note: during template argument deduction for class template partial specialization
'IsIterator<T, std::void_t<std::enable_if_t<std::is_base_of_v<std::input_iterator_tag, typename
std::iterator_traits<T>::iterator_category> > > >' [with T = void *]
return IsIterator<void*>::value;
^
main.cpp:29:16: note: in instantiation of template class 'IsIterator<void *, void>' requested here
为什么在这种情况下 std::void_t
不起作用?
std::iterator_traits<T>::iterator_category
强制为 void*
.
实例化格式不正确的 std::iterator_traits<T>
(SFINAE 的硬错误而非软错误)
您必须手动处理 void*
。
#include <type_traits>
template<typename, typename = void>
struct IsIterator final : std::false_type
{};
template<typename T>
struct IsIterator<T,
std::void_t<std::enable_if_t<std::is_base_of_v<std::input_iterator_tag,
typename std::iterator_traits<T>::iterator_category>>>>
final : std::true_type
{};
int main()
{
return IsIterator<void*>::value;
}
clang 8.0 给出以下错误信息:
/usr/bin/../include/c++/v1/iterator:507:16: error: cannot form a reference to 'void'
typedef _Tp& reference;
^
main.cpp:20:23: note: in instantiation of template class 'std::__1::iterator_traits<void *>' requested
here
typename std::iterator_traits<T>::iterator_category>>>>
^
main.cpp:29:16: note: during template argument deduction for class template partial specialization
'IsIterator<T, std::void_t<std::enable_if_t<std::is_base_of_v<std::input_iterator_tag, typename
std::iterator_traits<T>::iterator_category> > > >' [with T = void *]
return IsIterator<void*>::value;
^
main.cpp:29:16: note: in instantiation of template class 'IsIterator<void *, void>' requested here
为什么在这种情况下 std::void_t
不起作用?
std::iterator_traits<T>::iterator_category
强制为 void*
.
std::iterator_traits<T>
(SFINAE 的硬错误而非软错误)
您必须手动处理 void*
。