gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:27:45 24 4
gpt4 key购买 nike

我正在尝试使用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, std::void_t<decltype(std::declval<T>().begin()),
^

main.cpp(11): error: expected a ">"
struct is_iterable<T, std::void_t<decltype(std::declval<T>().begin()),
^

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的支持。

关于c++ - 示例void_t示例无法使用intel编译器19.0.4进行编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63562607/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com