示例 void_t 示例无法使用英特尔编译器 19.0.4 进行编译
sample void_t example does not compile with intel compiler 19.0.4
我正在尝试使用英特尔编译器编译 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.
难道2019版的icpc编译不了void_t?这看起来很奇怪,因为它适用于 Godbolt:https://godbolt.org/z/xoT8vr
该问题与编译器无关,而是与其配合使用的 C++ 标准库的实现有关。例如,在 Linux 系统上,Intel icpc
通常使用 system-installed libstdc++。请注意 support for std::void_t
was added into libstdc++ in version 6.1.
虽然 icpc
on Godbold uses libstdc++ version 8,但您的系统可能安装了一些不支持 std::void_t
的旧版本。
我正在尝试使用英特尔编译器编译 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.
难道2019版的icpc编译不了void_t?这看起来很奇怪,因为它适用于 Godbolt:https://godbolt.org/z/xoT8vr
该问题与编译器无关,而是与其配合使用的 C++ 标准库的实现有关。例如,在 Linux 系统上,Intel icpc
通常使用 system-installed libstdc++。请注意 support for std::void_t
was added into libstdc++ in version 6.1.
虽然 icpc
on Godbold uses libstdc++ version 8,但您的系统可能安装了一些不支持 std::void_t
的旧版本。