Home > Blockchain >  what is the way to remove the first element from a std::span<T>?
what is the way to remove the first element from a std::span<T>?

Time:02-03

when reading the document of std::span, I see there is no method to remove the first element from the std::span<T>.

Can you suggest a way to solve my issue?

The large picture of my problem(I asked in another question: How to instantiatiate a std::basic_string_view with custom class T, I got is_trivial_v<_CharT> assert error) is that I would like to have a std::basic_string_view<Token>, while the Token is not a trivial class, so I can't use std::basic_string_view, and someone suggested me to use std::span<Token> instead.

Since the basic_string_view has a method named remove_prefix which remove the first element, while I also need such kinds of function because I would like to use std::span<Token> as a parser input, so the Tokens will be matched, and consumed one by one.

Thanks.

CodePudding user response:

Call subspan with 1 as only (template) argument to get a new span, which doesn't contain the first element.

CodePudding user response:

You can write remove_prefix of your version,

template <typename T>
constexpr void remove_prefix(std::span<T>& sp, std::size_t n) {
    sp = sp.subspan(n);
}

Demo

  • Related