考虑以下无用的代码:
#include <ranges>
#include <source_location>
#include <iostream>
int main() {
auto lines = std::views::iota(0, 5)
| std::views::transform(
[](int, const std::source_location& location = std::source_location::current())
{ return location.line(); }
);
for (const auto& line : lines)
std::cout << line << "\n";
}
MSVC 拒绝并显示奇怪的错误消息:
(7): error C2676: binary '|': 'std::ranges::iota_view<_Ty1,_Ty2>' does not define this operator or a conversion to a type acceptable to the predefined operator
with
[
_Ty1=int,
_Ty2=int
]
61
无论在哪一行,GCC 都会输出奇怪的行号std::source_location::current()
:
61
61
61
61
61
上面的代码格式正确吗?如果是这样,是否意味着 MSVC 和 GCC 都有错误?