I am designing a function in C++20 that needs to be callable on both containers and views. However, I want to ensure thread safety and avoid concurrency issues when the function is called in parallel on the same view / container. The function does not perform any modifications and simply iterates over the view / container.
One option is to use std::ranges::range auto &&
, as the parameter type for the function, allowing both containers and views to be passed. However, this raises concerns about potential concurrency issues if the function operates on a view, since iterating over a view in parallel is not necessarily thread safe.
Another option is to use std::ranges::view auto
, as the parameter type, which guarantees thread safety since the view gets copied. However, this restricts the function's usage to only views and not containers directly.
What would be the recommended approach to design the function interface in this scenario?