How to use std :: string_view :: remove_prefix () in constexpr string_view

std::string_view::remove_prefix()and std::string_view::remove_suffix()are constexprmember functions in C ++ 17; however, they change the variable they invoke. If a value constexpr, it will constalso not be editable, so how can these functions be used for a value constexpr?

In other words:

constexpr std::string_view a = "asdf";
a.remove_prefix(2); // compile error- a is const

How do you use these features on constexpr std::string_view? If they cannot be used on constexpr std::string_view, why are the functions themselves marked constexpr?

+4
source share
1 answer

, constexpr, , constexpr, :

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);

remove_prefix() constexpr, .


, :

constexpr std::string_view a = "asdf"sv.substr(2);
+7

Source: https://habr.com/ru/post/1680133/


All Articles