1

I'd like to have function that accepts any container of a fixed type. For a example a function that will accept both std::array<float,1> and std::array<float,2>.

I thought this would be possible with ranges but I'm realizing my understanding is quite superficial.

I this possible without templates?


Edit: Can we define a type using the ranges library that will do the equivalent of span but will work for non-contiguous containers? Maybe I didn't phrase my question right, I probably meant view rather than container.

Tom Huntington
  • 2,260
  • 10
  • 20
  • 2
    [`std::span`](https://en.cppreference.com/w/cpp/container/span)``? – Jarod42 Jul 29 '21 at 10:09
  • `std::array` and `std::array` are distinct types. You can't write a non-generic function that accepts both. If you don't want to make your function a template, you effectively need to make the type you're gonna be taking type-erase the original container for you - but that will likely use templates internally. – Bartek Banachewicz Jul 29 '21 at 10:12

1 Answers1

5

For contiguous ranges, you might use std::span (C++20):

void foo(std::span<float>)
{
// ...
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302