If I have const and non-const versions of a function that returns a span:
class Foo {
public:
std::span<int> myFunc() {
auto begin{vec.begin()};
auto end{vec.end()};
// Do some work that modifies begin and end.
return {begin, end};
}
std::span<const int> myFunc() const {
// Same as above
}
private:
std::vector<int> vec{1, 2, 3};
};
Is there any way to avoid duplicating the function bodies?
I tried the const_cast
approach, but got stuck on trying to cast the std::span
return value.