简体   繁体   English

为什么在这种情况下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 gives the following error messages: 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

Why does std::void_t not work in such a case? 为什么在这种情况下std::void_t不起作用?

std::iterator_traits<T>::iterator_category forces instantiation of std::iterator_traits<T> which is ill formed (hard error and not soft error for SFINAE) for void* . std::iterator_traits<T>::iterator_category强制实例化std::iterator_traits<T> (对于void* ,格式不正确(对于SFINAE是硬错误而不是软错误))。

You have to handle void* manually. 您必须手动处理void*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM