I have the following piece of code:
#include <iostream>
#include <string>
#include <ranges>
#include <algorithm>
int main()
{
using std::string, std::string_view;
using std::cout, std::endl;
// auto = std::initializer_list<const char*>
auto vec = { "A", "B", "a", "b", "b2", "a2", "a3", "b3", "b4" };
for (std::size_t i = 0; string_view val : vec
| std::views::reverse
| std::views::take_while([](string_view s) { return s.size() > 0 && s[0] == 'b'; })
| std::views::transform([](string s) {
std::transform(s.cbegin(), s.cend(), s.begin(), (int (*)(int))std::toupper);
return s;
})
| std::views::reverse
)
{
cout << i++ << " - " << val << endl;
}
}
When I compile it with g++ -std=c++20 main.cpp -o main
it works fine.
However when I compile it with clang -std=c++20 main.cpp -o main
it throws 3 pages of super cryptic error messages at me. Why is that?
g++ (Ubuntu 11.2.0-19ubuntu1) 11.2.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Ubuntu clang version 15.0.0-++20220620042224+362814d2247e-1~exp1~20220620042313.247
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
I don't wish to use clang to compile, but my IDE, clion, uses clangd as a language server and it's bugging me about those errors. I have also tried -std=gnu++20
and std-=gnu++2b
, but they give the same errors. I have tried adding -query-driver=/usr/bin/g++
to the "diagnostics flags" in the clangd settings, but nothing changed.
Aren't compilers supposed to work on the same code given a specific standard? Is one of them not following that standard? What's going on?
Errors by clang below:
In file included from main.cpp:1:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/iostream:39:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ostream:38:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ios:40:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/char_traits.h:39:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/stl_algobase.h:65:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/stl_iterator_base_types.h:71:
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/iterator_concepts.h:979:13: error: no matching function for call to '__begin'
= decltype(ranges::__cust_access::__begin(std::declval<_Tp&>()));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/ranges_base.h:586:5: note: in instantiation of template type alias '__range_iter_t' requested here
using iterator_t = std::__detail::__range_iter_t<_Tp>;
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/ranges_util.h:98:43: note: in instantiation of template type alias 'iterator_t' requested here
data() requires contiguous_iterator<iterator_t<_Derived>>
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:1021:29: note: in instantiation of template class 'std::ranges::view_interface<std::ranges::ref_view<std::initializer_list<const char *>>>' requested here
class ref_view : public view_interface<ref_view<_Range>>
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:1077:38: note: in instantiation of template class 'std::ranges::ref_view<std::initializer_list<const char *>>' requested here
concept __can_ref_view = requires { ref_view{std::declval<_Range>()}; };
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:1077:38: note: in instantiation of requirement here
concept __can_ref_view = requires { ref_view{std::declval<_Range>()}; };
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:1077:27: note: (skipping 14 contexts in backtrace; use -ftemplate-backtrace-limit=0 to see all)
concept __can_ref_view = requires { ref_view{std::declval<_Range>()}; };
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:755:9: note: while substituting template arguments into constraint expression here
= requires { std::declval<_Adaptor>()(declval<_Args>()...); };
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:780:5: note: while checking the satisfaction of concept '__adaptor_invocable<const std::ranges::views::_Reverse &, std::initializer_list<const char *> &>' requested here
&& __adaptor_invocable<_Self, _Range>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:780:5: note: while substituting template arguments into constraint expression here
&& __adaptor_invocable<_Self, _Range>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:52:7: note: while checking constraint satisfaction for template 'operator|<const std::ranges::views::_Reverse &, std::initializer_list<const char *> &>' required here
| std::views::reverse
^
main.cpp:52:7: note: in instantiation of function template specialization 'std::ranges::views::__adaptor::operator|<const std::ranges::views::_Reverse &, std::initializer_list<const char *> &>' requested here
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/iterator_concepts.h:963:7: note: candidate template ignored: constraints not satisfied [with _Tp = std::ranges::ref_view<std::initializer_list<const char *>>]
__begin(_Tp& __t)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/iterator_concepts.h:961:16: note: because 'is_array_v<std::ranges::ref_view<std::initializer_list<const char *> > >' evaluated to false
requires is_array_v<_Tp> || __member_begin<_Tp&> || __adl_begin<_Tp&>
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/iterator_concepts.h:961:35: note: and 'std::ranges::ref_view<std::initializer_list<const char *>> &' does not satisfy '__member_begin'
requires is_array_v<_Tp> || __member_begin<_Tp&> || __adl_begin<_Tp&>
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/iterator_concepts.h:944:23: note: because '__decay_copy(__t.begin())' would be invalid: no member named 'begin' in 'std::ranges::ref_view<std::initializer_list<const char *>>'
{ __decay_copy(__t.begin()) } -> input_or_output_iterator;
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/iterator_concepts.h:961:59: note: and 'std::ranges::ref_view<std::initializer_list<const char *>> &' does not satisfy '__adl_begin'
requires is_array_v<_Tp> || __member_begin<_Tp&> || __adl_begin<_Tp&>
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/iterator_concepts.h:955:19: note: because '__decay_copy(begin(__t))' would be invalid: call to deleted function 'begin'
{ __decay_copy(begin(__t)) } -> input_or_output_iterator;
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/iterator_concepts.h:979:13: error: no matching function for call to '__begin'
= decltype(ranges::__cust_access::__begin(std::declval<_Tp&>()));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/ranges_base.h:586:5: note: in instantiation of template type alias '__range_iter_t' requested here
using iterator_t = std::__detail::__range_iter_t<_Tp>;
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/ranges_util.h:104:25: note: in instantiation of template type alias 'iterator_t' requested here
&& contiguous_iterator<iterator_t<const _Derived>>
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:1021:29: note: in instantiation of template class 'std::ranges::view_interface<std::ranges::ref_view<std::initializer_list<const char *>>>' requested here
class ref_view : public view_interface<ref_view<_Range>>
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:1077:38: note: in instantiation of template class 'std::ranges::ref_view<std::initializer_list<const char *>>' requested here
concept __can_ref_view = requires { ref_view{std::declval<_Range>()}; };
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:1077:38: note: in instantiation of requirement here
concept __can_ref_view = requires { ref_view{std::declval<_Range>()}; };
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:1077:27: note: (skipping 14 contexts in backtrace; use -ftemplate-backtrace-limit=0 to see all)
concept __can_ref_view = requires { ref_view{std::declval<_Range>()}; };
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:755:9: note: while substituting template arguments into constraint expression here
= requires { std::declval<_Adaptor>()(declval<_Args>()...); };
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:780:5: note: while checking the satisfaction of concept '__adaptor_invocable<const std::ranges::views::_Reverse &, std::initializer_list<const char *> &>' requested here
&& __adaptor_invocable<_Self, _Range>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:780:5: note: while substituting template arguments into constraint expression here
&& __adaptor_invocable<_Self, _Range>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:52:7: note: while checking constraint satisfaction for template 'operator|<const std::ranges::views::_Reverse &, std::initializer_list<const char *> &>' required here
| std::views::reverse
^
main.cpp:52:7: note: in instantiation of function template specialization 'std::ranges::views::__adaptor::operator|<const std::ranges::views::_Reverse &, std::initializer_list<const char *> &>' requested here
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/iterator_concepts.h:963:7: note: candidate template ignored: constraints not satisfied [with _Tp = conststd::ranges::ref_view<std::initializer_list<const char *>>]
__begin(_Tp& __t)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/iterator_concepts.h:961:16: note: because 'is_array_v<const std::ranges::ref_view<std::initializer_list<const char *> > >' evaluated to false
requires is_array_v<_Tp> || __member_begin<_Tp&> || __adl_begin<_Tp&>
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/iterator_concepts.h:961:35: note: and 'const std::ranges::ref_view<std::initializer_list<const char *>> &' does not satisfy '__member_begin'
requires is_array_v<_Tp> || __member_begin<_Tp&> || __adl_begin<_Tp&>
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/iterator_concepts.h:944:23: note: because '__decay_copy(__t.begin())' would be invalid: no member named 'begin' in 'std::ranges::ref_view<std::initializer_list<const char *>>'
{ __decay_copy(__t.begin()) } -> input_or_output_iterator;
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/iterator_concepts.h:961:59: note: and 'const std::ranges::ref_view<std::initializer_list<const char *>> &' does not satisfy '__adl_begin'
requires is_array_v<_Tp> || __member_begin<_Tp&> || __adl_begin<_Tp&>
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/iterator_concepts.h:955:19: note: because '__decay_copy(begin(__t))' would be invalid: call to deleted function 'begin'
{ __decay_copy(begin(__t)) } -> input_or_output_iterator;
^
In file included from main.cpp:3:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:46:
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/ranges_util.h:110:24: error: constraints not satisfied for alias template 'sentinel_t' [with _Range = std::ranges::ref_view<std::initializer_list<const char *>>]
&& sized_sentinel_for<sentinel_t<_Derived>, iterator_t<_Derived>>
^~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:1021:29: note: in instantiation of template class 'std::ranges::view_interface<std::ranges::ref_view<std::initializer_list<const char *>>>' requested here
class ref_view : public view_interface<ref_view<_Range>>
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:1077:38: note: in instantiation of template class 'std::ranges::ref_view<std::initializer_list<const char *>>' requested here
concept __can_ref_view = requires { ref_view{std::declval<_Range>()}; };
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:1077:38: note: in instantiation of requirement here
concept __can_ref_view = requires { ref_view{std::declval<_Range>()}; };
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:1077:27: note: while substituting template arguments into constraint expression here
concept __can_ref_view = requires { ref_view{std::declval<_Range>()}; };
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:1087:7: note: while checking the satisfaction of concept '__can_ref_view<std::initializer_list<const char *> &>' requested here
|| __detail::__can_ref_view<_Range>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:1087:7: note: (skipping 12 contexts in backtrace; use -ftemplate-backtrace-limit=0 to see all)
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:755:9: note: while substituting template arguments into constraint expression here
= requires { std::declval<_Adaptor>()(declval<_Args>()...); };
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:780:5: note: while checking the satisfaction of concept '__adaptor_invocable<const std::ranges::views::_Reverse &, std::initializer_list<const char *> &>' requested here
&& __adaptor_invocable<_Self, _Range>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:780:5: note: while substituting template arguments into constraint expression here
&& __adaptor_invocable<_Self, _Range>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:52:7: note: while checking constraint satisfaction for template 'operator|<const std::ranges::views::_Reverse &, std::initializer_list<const char *> &>' required here
| std::views::reverse
^
main.cpp:52:7: note: in instantiation of function template specialization 'std::ranges::views::__adaptor::operator|<const std::ranges::views::_Reverse &, std::initializer_list<const char *> &>' requested here
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/ranges_base.h:588:12: note: because 'std::ranges::ref_view<std::initializer_list<const char *>>' does not satisfy 'range'
template<range _Range>
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/ranges_base.h:576:2: note: because 'ranges::begin(__t)' would be invalid: no matching function for call to object of type 'const __cust_access::_Begin'
ranges::begin(__t);
^
In file included from main.cpp:3:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:46:
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/ranges_util.h:116:24: error: constraints not satisfied for alias template 'sentinel_t' [with _Range = const std::ranges::ref_view<std::initializer_list<const char *>>]
&& sized_sentinel_for<sentinel_t<const _Derived>,
^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/ranges_base.h:588:12: note: because 'const std::ranges::ref_view<std::initializer_list<const char *>>' does not satisfy 'range'
template<range _Range>
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/ranges_base.h:576:2: note: because 'ranges::begin(__t)' would be invalid: no matching function for call to object of type 'const __cust_access::_Begin'
ranges::begin(__t);
^
main.cpp:52:7: error: invalid operands to binary expression ('std::initializer_list<const char *>' and 'const std::ranges::views::_Reverse')
| std::views::reverse
^ ~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/ios_base.h:87:3: note: candidate function not viable: no known conversion from 'std::initializer_list<const char *>' to 'std::_Ios_Fmtflags' for 1st argument
operator|(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/ios_base.h:129:3: note: candidate function not viable: no known conversion from 'std::initializer_list<const char *>' to 'std::_Ios_Openmode' for 1st argument
operator|(_Ios_Openmode __a, _Ios_Openmode __b)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/ios_base.h:169:3: note: candidate function not viable: no known conversion from 'std::initializer_list<const char *>' to 'std::_Ios_Iostate' for 1st argument
operator|(_Ios_Iostate __a, _Ios_Iostate __b)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:782:7: note: candidate template ignored: constraints not satisfied [with _Self = const std::ranges::views::_Reverse &, _Range = std::initializer_list<const char *> &]
operator|(_Range&& __r, _Self&& __self)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:780:5: note: because '__adaptor_invocable<const std::ranges::views::_Reverse &, std::initializer_list<const char *> &>' evaluated to false
&& __adaptor_invocable<_Self, _Range>
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:755:20: note: because 'std::declval<_Adaptor>()(declval<_Args>()...)' would be invalid: no matching function for call to object of type 'const std::ranges::views::_Reverse'
= requires { std::declval<_Adaptor>()(declval<_Args>()...); };
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:791:7: note: candidate template ignored: constraints not satisfied [with _Lhs = std::initializer_list<const char *>, _Rhs = std::ranges::views::_Reverse]
operator|(_Lhs __lhs, _Rhs __rhs)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges:788:16: note: because 'derived_from<std::initializer_list<const char *>, std::ranges::views::__adaptor::_RangeAdaptorClosure>' evaluated to false
requires derived_from<_Lhs, _RangeAdaptorClosure>
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/concepts:67:28: note: because '__is_base_of(std::ranges::views::__adaptor::_RangeAdaptorClosure, std::initializer_list<const char *>)' evaluated to false
concept derived_from = __is_base_of(_Base, _Derived)
^
5 errors generated.