constexpr字符串视图的Initializer列表



我正在尝试制作一些字符串常量的constexpr初始值设定项列表。我认为这在C++17中应该是可能的,但我无法在MSVC中编译它。

#include <string_view>
using namespace std::literals::string_view_literals;
const constexpr std::initializer_list<std::string_view> some_strings{
"asdf"sv
};

这会给出以下错误消息:

error C2131: expression did not evaluate to a constant
note: failure was caused by non-constant arguments or reference to a non-constant symbol
note: see usage of '$S1'

我不知道哪一部分会是非常量

  • std::string_view的运算符"是constexpr
  • std::initializer_list的构造函数也是constexpr

有什么想法吗?这可能只是visualstudio中的一个bug吗?错误消息中引用的"$S1"是什么?

编辑:Visual Studio 15.8.9

这似乎是旧版本上的一个错误:https://godbolt.org/z/YICeqi

我刚刚在v15.9.2上试用了你的代码,它运行得很好。

最新更新