示例void_t示例无法使用intel编译器19.0.4进行编译

问题描述

我正在尝试使用intel编译器编译void_t的cppreference示例代码:

#include <iostream>
#include <type_traits>
#include <vector>
#include <map>
 
class A {};
 
template <typename T,typename = void>
struct is_iterable : std::false_type {};
template <typename T>
struct is_iterable<T,std::void_t<decltype(std::declval<T>().begin()),decltype(std::declval<T>().end())>>
    : std::true_type {};
 
// An iterator trait which value_type is always the value_type of the 
// iterated container,even with back_insert_iterator which value_type is void
 
template <typename T,typename = void>
struct iterator_trait 
: std::iterator_traits<T> {};
template <typename T>
struct iterator_trait<T,std::void_t<typename T::container_type>> 
: std::iterator_traits<typename T::container_type::iterator> {};
 
int main()
{
    std::cout << std::boolalpha;
    std::cout << is_iterable<std::vector<double>>::value << '\n';
    std::cout << is_iterable<std::map<int,double>>::value << '\n';
    std::cout << is_iterable<double>::value << '\n';
    std::cout << is_iterable<A>::value << '\n';
 
 
    std::vector<int> v;
 
    std::cout << std::is_same<iterator_trait<decltype(std::back_inserter(v))>::value_type,iterator_trait<decltype(v.cbegin())>::value_type >::value << '\n';
}

我遇到以下错误:

main.cpp(11): error: namespace "std" has no member "void_t"
  struct is_iterable<T,^

main.cpp(11): error: expected a ">"
  struct is_iterable<T,^

main.cpp(12): error: expected a ";"
                                    decltype(std::declval<T>().end())>>
                                                                     ^

main.cpp(22): error: namespace "std" has no member "void_t"
  struct iterator_trait<T,std::void_t<typename T::container_type>>
                                ^

main.cpp(22): error: expected a ">"
  struct iterator_trait<T,std::void_t<typename T::container_type>>
                                      ^

main.cpp(22): error: expected a ";"
  struct iterator_trait<T,std::void_t<typename T::container_type>>
                                                                 ^

compilation aborted for main.cpp (code 2)

我使用以下行进行编译:

icpc -std=c++17 main.cpp

我的icpc版本是:

icpc --version
icpc (ICC) 19.0.4.243 20190416
Copyright (C) 1985-2019 Intel Corporation.  All rights reserved.

这是否意味着icpc的2019版本不会编译void_t?这似乎很奇怪,因为它可以在Godbolt上运行:https://godbolt.org/z/xoT8vr

解决方法

问题与编译器无关,但与它一起使用的C ++标准库的实现有关。例如,在Linux系统上,英特尔icpc通常使用系统安装的libstdc ++。请注意,support for std::void_t was added into libstdc++ in version 6.1

icpc on Godbold uses libstdc++ version 8期间,您的系统可能安装了一些不支持std::void_t的较旧版本。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...