这是std :: basic_string_view的assignment运算符的定义

constexpr basic_string_view& operator=(const basic_string_view& view) noexcept = default;

有人可以向我解释为赋值运算符使用constexpr的目的是什么?

更普遍的问题是使可变成员constexpr的原因是什么?使用VS2015编译器时,我有一个警告,例如


  在C ++ 14中,“ constexpr”不会暗示“ const”;考虑明确指定“ const”


应该不是错误吗?

最佳答案

您可以在constexpr上下文中创建局部变量,然后在C ++ 14中对其进行修改。

但是,如果赋值运算符不是constexpr,则无法使用它。

template<class T, std::size_t N>
constexpr std::array<T, N> sort( std::array<T, N> in ) {
  for (std::size_t i = 0; i < in.size(); ++i) {
    for (std::size_t j = i+1; j < in.size(); ++j) {
      if (in[i] > in[j]) {
        auto tmp = in[j];
        in[j] = in[i];
        in[i] = tmp;
      }
    }
  }
  return in;
}


live example

07-28 01:25
查看更多