Make function template of span<auto> accept collections with inferrence of element type at call sites

51 Views Asked by At

I'd like to be able to pass values of as wide as possible a range of types to functions based on a template accepting span. I've got the following code:

#include <iostream>
#include <span>
using namespace std;

void f(span<auto> s) {
    for(auto &a : s)
        cout << a << endl;
}

template<class T>
void h(T &&x) {
    f<remove_reference_t<decltype(x[0])>>(forward<T>(x));
}

int main() {
    int a[]{5, 6};
    auto b = array{7, 9, 3, 1};
    h(a);
    h(b);
}

It works, but I expected to be able to do it without the helper function h, just with argument initialization, thus: f({a}), f({b}). Is there a way to achieve it or something similar?

0

There are 0 best solutions below