I'm trying to compile the cppreference example code of void_t with intel compiler :
#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';
}
I have the following error :
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)
I use the following line for compilation :
icpc -std=c++17 main.cpp
and my icpc version is :
icpc --version
icpc (ICC) 19.0.4.243 20190416
Copyright (C) 1985-2019 Intel Corporation. All rights reserved.
Is that mean that the 2019 version of icpc does not compile void_t ? That seems strange because it works on godbolt : https://godbolt.org/z/xoT8vr
The problem is not related to a compiler but to the implementation of the C++ Standard Library it works with. For instance, on Linux systems, Intel icpc
typically uses system-installed libstdc++. Note that the support for std::void_t
was added into libstdc++ in version 6.1.
While icpc
on Godbold uses libstdc++ version 8, your system likely have some older version installed without the support for std::void_t
.