【发布时间】:2021-12-15 11:07:50
【问题描述】:
根据https://en.cppreference.com/w/cpp/ranges/split_view,std::ranges::split_view 必须从 C++20 开始可用。但是,同一页面上的示例在其文本中包含“C++23”:
#include <iostream>
#include <iomanip>
#include <ranges>
#include <string_view>
int main() {
constexpr std::string_view words{"Hello-_-C++-_-23-_-!"};
constexpr std::string_view delim{"-_-"};
for (const std::string_view word : std::ranges::split_view(words, delim)) {
std::cout << std::quoted(word) << ' ';
}
}
GCC 和 MSVC 都拒绝接受这个 C++20 模式下的例子。 MSVC 特别打印:
The contents of <ranges> are available only in c++latest mode with concepts support;
https://gcc.godbolt.org/z/4fGGb3aqY
GCC 开始接受带有 -std=c++2b 命令行开关的代码(意味着即将推出的 C++23),而 MSVC 即使带有 /std:c++latest 选项也会报告错误
error C2440: 'initializing': cannot convert from 'std::ranges::split_view<std::basic_string_view<char,std::char_traits<char>>,std::basic_string_view<char,std::char_traits<char>>>::_Outer_iter<true>::value_type' to 'std::basic_string_view<char,std::char_traits<char>>'
note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
演示:https://gcc.godbolt.org/z/6z48Mz45j
C++20模式下的例子有什么问题,还是真的需要编译器的一些C++23特性?
【问题讨论】:
-
别写
ranges::split_view,写views::split。 -
别被
的内容只有在c++latest模式下才支持concepts; ,不代表<ranges>是不是 c++20,这意味着 MSVC STL 实现者决定等到<ranges>稳定:github.com/microsoft/STL/issues/1814#issuecomment-845572895
标签: c++ c++20 std-ranges c++23