You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

std::ranges系列算法为何无法适配std::initializer_list?

std::ranges系列算法是否应该支持std::initializer_list?

使用x64 MSVC v19.latest编译器并开启/std:c++latest选项时,以下代码中(B)行可正常编译,(A)行却无法通过编译:

#include <algorithm>
#include <array>
#define ONE 1
#define TWO 2
static_assert(std::ranges::is_sorted({ ONE, TWO }));  // (A) 无法编译
static_assert(std::ranges::is_sorted(std::array { ONE, TWO }));  // (B) 编译通过
int main() {}

std::initializer_list分配的底层临时数组本应存活到is_sorted()调用结束,但编译器报错显示std::initializer_list无法转换为std::ranges::forward_range,具体错误提示为:
algorithm(10432): 'bool std::ranges::_Is_sorted_fn::operator ()(_Rng &&,_Pr,_Pj) const': could not deduce template argument for '_Rng'

请问这一问题是否与Stack Overflow上的相关问题原因相同?

内容的提问来源于stack exchange,提问作者Frank Heimes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 15:35:28