Turns out std::span
has some deduction limitations. There's a pending paper proposing a fix which might be applied in C++26. In the meantime, we can use a helper factory function (e.g. as_span
below) to enable this missing deduction capability. We could alternatively apply the fix directly to pw::span
, but upstreaming will be controversial.
// as_span implementation snipped - view in godbolt link below
template<typename T>
void foo(std::span<const T>);
void test() {
// Can never deduce foo<T>()
// None of these work:
#if 0
foo({1,2,3});
foo<int>({1,2,3});
foo(std::array{1,2,3});
foo(std::array<int,3>{1,2,3});
#endif
// Only this works:
foo<int>(std::array{1,2,3});
// Can always deduce foo<T>() with as_span()
// All of these work:
foo(as_span({1,2,3}));
foo<int>(as_span({1,2,3}));
foo(as_span(std::array{1,2,3}));
foo(as_span(std::array<int,3>{1,2,3}));
foo<int>(as_span(std::array{1,2,3}));
}
https://godbolt.org/z/5YGfYarYM
Related links: