In my code, I would like to create a new class named Span
by extending the std::span
class, but the new Span
has some extra member functions which does not exist in std::span
.
One is adding the remove_prefix
function to std::span
, which is implemented and works OK. See my previous question for details: what is the way to remove the first element from a std::span?
Now I would like to add another member function named substr
(I try to imitates some member functions of std::string_view
because I need to support both std::span<T>
and std::string_view
in my design). In the old code, I have only std::string_view
support, so it has some code snippet like sv.substr()
, now I would like sv
could be general, which means it could be either std::string_view
or std::span<T>
(Span<T>
).
Here is the minimal sample code I used to solve this issue:
#include <iostream>
#include <span>
using namespace std;
// Span class which is derived class from std::span
// add remove_prefix and substr function which is similar like std::string_view
template<typename T>
class Span : public std::span<T>
{
public:
// Inheriting constructors from std::span
using std::span<T>::span;
// add a public function which is similar to std::string_view::remove_prefix
constexpr void remove_prefix(std::size_t n) {
auto& self = static_cast<std::span<T>&>(*this);
self = self.subspan(n);
}
// add another public function substr, which is the span::subspan
constexpr Span substr(std::size_t pos, std::size_t count){
auto& self = static_cast<std::span<T>&>(*this);
auto ret = self.subspan(pos, count);
return static_cast<Span<T>&>(ret);
}
};
float arr[3] = {1.0, 2.0, 3.0};
int main()
{
Span<float> a(arr, 2);
Span<float> b = a.substr(0, 1);
return 0;
}
The above code builds and runs OK under g++ with C++20 support. My question is: is my implementation correct? Because I use so many static_cast
in the code. In-fact, std::span
already has a function named subspan
, what I do is just make a function name alias substr
.
Any ideas? Thanks.