I would like to use my user defined concept as a template type of std::span but template argument deduction does not work as I expected. When I try to pass a "std::array
of char
" to template function; compiler shows an error "error: no matching function for call to 'print'" and warns me when I hover over the template definition with mouse as "note: candidate template ignored: could not match 'span' against 'array'".
Here is the concept definition and function template:
#include <concepts>
#include <span>
template <typename T>
concept OneByteData = sizeof(T) == 1;
template<OneByteData T>
void print(std::span<const T> container)
{
for(auto element : container)
{
//Do Some Work
}
}
And the user code that doesn't work as I expected:
int main()
{
std::array<char, 6> arr = {1, 2, 3, 4, 5, 6};
print(arr);
return 0;
}
The user code that works and does not produce error:
int main()
{
std::array<char, 6> arr = {1, 2, 3, 4, 5, 6};
print<char>(arr);
return 0;
}
Is there way to call this template function without specializing the type of array. How should I change the template function definition to make the function call the way I mentioned (print(arr)
) ?
Edit: I would like to be able to use the benefits of std::span and be able to call the template function with std::array, std::vector and plain C-style arrays.