I have the following code:
#include <span>
int* pointers[3];
std::span<const int* const> s = pointers;
GCC accepts this, but Clang (compiling libstdc++) rejects it, stating:
<source>:4:29: error: no viable conversion from 'int *[3]' to 'std::span<const int *const>'
4 | std::span<const int* const> s = pointers;
| ^ ~~~~~~~~
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/15.0.0/../../../../include/c++/15.0.0/span:230:7: note: candidate constructor not viable: no known conversion from 'int *[3]' to 'const span<const int *const> &' for 1st argument
230 | span(const span&) noexcept = default;
| ^ ~~~~~~~~~~~
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/15.0.0/../../../../include/c++/15.0.0/span:192:2: note: candidate template ignored: could not match 'const int *' against 'int *'
192 | span(type_identity_t<element_type> (&__arr)[_ArrayExtent]) noexcept
| ^
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/15.0.0/../../../../include/c++/15.0.0/span:199:2: note: candidate template ignored: could not match 'array<_Tp, _ArrayExtent>' against 'int *[3]'
199 | span(array<_Tp, _ArrayExtent>& __arr) noexcept
| ^
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/15.0.0/../../../../include/c++/15.0.0/span:206:2: note: candidate template ignored: could not match 'array<_Tp, _ArrayExtent>' against 'int *[3]'
206 | span(const array<_Tp, _ArrayExtent>& __arr) noexcept
| ^
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/15.0.0/../../../../include/c++/15.0.0/span:218:2: note: candidate template ignored: constraints not satisfied [with _Range = int *(&)[3]]
218 | span(_Range&& __range)
| ^
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/15.0.0/../../../../include/c++/15.0.0/span:213:8: note: because '!is_array_v<remove_cvref_t<int *(&)[3]> >' evaluated to false
213 | && (!is_array_v<remove_cvref_t<_Range>>)
| ^
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/15.0.0/../../../../include/c++/15.0.0/span:238:2: note: candidate template ignored: could not match 'span<_OType, _OExtent>' against 'int *[3]'
238 | span(const span<_OType, _OExtent>& __s) noexcept
| ^
See Compiler Explorer
I was under the impression that std::span
can be constructed from ranges if only a qualification conversion (such as int*
to const int * const
) is required, so which compiler is correct here?
This is quite likely just a Clang/GCC interoperability quirk, considering that the code also compiles with -stdlib=libc++
.