std::string_view::remove_prefix()
和std::string_view::remove_suffix()
是c++17中的两个constexpr
成员函数;然而,它们会修改被调用的变量。如果这个值是constexpr
,那么它也是const
的,并且不能被修改,那么如何在一个constexpr
值上使用这些函数呢?
换句话说:
constexpr std::string_view a = "asdf";
a.remove_prefix(2); // compile error- a is const
如何在constexpr std::string_view
上使用这些函数?如果它们不能在constexpr std::string_view
上使用,为什么这些函数本身被标记为constexpr
?
constexpr void remove_prefix(size_type)
。你能在 constexpr 中使用一个唯一目的是改变现有对象的方法吗? - Uselessremove_prefix()
实现没有标记为constexpr
。 - Barryconstexpr
意味着const
,不是吗?所以std::string_view::remove_prefix()
不应该能够修改自身吧? - inetknghtconstexpr
成员函数不需要是const
成员函数。 - Barry