std::string_view::remove_prefix() and std::string_view::remove_suffix() are both constexpr member functions in c++17; however, they modify the variable on which they are called. If the value is constexpr, it will also be const and cannot be modified, so how can these functions be used on a constexpr value?
To put it another way:
constexpr std::string_view a = "asdf";
a.remove_prefix(2); // compile error- a is const
How do you use these functions on a constexpr std::string_view? If they can't be used on a constexpr std::string_view, why are the functions themselves marked constexpr?
The reason they're marked constexpr is so you can use them within a constexpr function, e.g.:
constexpr std::string_view remove_prefix(std::string_view s) {
s.remove_prefix(2);
return s;
}
constexpr std::string_view b = remove_prefix("asdf"sv);
If remove_prefix() weren't constexpr, this would be an error.
That said, I would write:
constexpr std::string_view a = "asdf"sv.substr(2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With