CPlusPlusHowToProgram11e
CPlusPlusHowToProgram11e copied to clipboard
fig15_20 compile error
the following codes can't compile:
const std::array ints1{1, 2, 3, 4, 5}; // has random-access iterators
auto result1{customDistance(ints1.begin(), ints1.end())};
template <std::input_iterator Iterator>
auto customDistance(Iterator begin, Iterator end) {
// for random-access iterators subtract the iterators
if constexpr (std::is_base_of_v<std::random_access_iterator_tag,
typename Iterator::iterator_category>) {
std::cout << "customDistance with random-access iterators\n";
return end - begin; // O(1) operation for random-access iterators
}
I am using g++-13 and the error messages are:
$ g++-13 -std=c++20 fig15_20.cpp -o fig15_20
fig15_20.cpp: In instantiation of ‘auto customDistance(Iterator, Iterator) [with Iterator = const int*]’:
fig15_20.cpp:40:31: required from here
fig15_20.cpp:16:23: error: ‘const int*’ is not a class, struct, or union type
16 | if constexpr (std::is_base_of_v<std::random_access_iterator_tag,
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17 | typename Iterator::iterator_category>) {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
It seems the Iterator of std::array is a raw int *. std::is_base_of_v need a class/struct/union type.
If I changed std::array to std::vector, it works.
And I searched C++20 std::random_access_iterator is a better way:
if constexpr (std::random_access_iterator<Iterator>) {
std::cout << "customDistance with random-access iterators\n";
return end - begin; // O(1) operation for random-access iterators
}
It work well with both std::array and std::vector.