错误,我写了线沿线的东西constexpr bool{};
,虽然GCC和锵拒绝了这个,MSVC更乐意编译它(见Godbolt)。根据我的理解,在编译时评估的函数(以及构造函数)不会有副作用,因此这永远不会有任何影响,但它确实是格式错误的吗?
(根据我的经验,MSVC 往往是错误的,但在这种特定情况下,我没有找到标准禁止的地方。)
考虑以下代码(单击此处获取 Godbolt):
#include <algorithm>
#include <ranges>
#include <vector>
int main() {
auto v = std::vector<short>{1, 2};
auto view = v | std::views::transform([] (auto i) { return static_cast<int>(i); });
auto it = view.begin() + 1;
auto prev_it = std::ranges::prev(it); //this one is fine
//auto prev_it = std::prev(it); //this one dies with an infinite loop
return *prev_it;
}
Run Code Online (Sandbox Code Playgroud)
主要问题:调用std::prev
而不是调用std::ranges::prev
迭代器会使 gcc 陷入无限循环。这意味着存在编译器错误或代码调用std::prev
调用了未定义的行为——这是哪一个?
关于后者的一些想法:std::prev
需要LegacyBidirectionalIterator,而std::ranges::prev
需要概念bidirectional_iterator。从我的理解,这两者之间的唯一区别是(从说明采取bidirectional_iterator): …