根据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 功能?