#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
强制实例化格式错误的std::iterator_traits<T>
(SFINAE 的硬错误而不是软错误(void*
。
您必须手动处理void*
。