c++ - std::ranges::to 是否允许转换为 std::map?

std::ranges::to 论文 wg21.link/p1206 中,概述部分具有以下内容

//Supports converting associative container to sequence containers
auto f = ranges::to<vector>(m);

但是我找不到在论文的其余部分中描述转换为 std::map 的细节的地方。我在 https://github.com/TartanLlama/ranges 中尝试了 range-v3 和 Sy Brand 的 ranges::to 实现。并且它们都不编译将范围转换为 std::map 的代码。那么这只是这些库中缺少的,还是正在转换为并非真正允许的 std::map

最佳答案

Does std::ranges::to allow converting to a std::map?

是的。

I tried range-v3 and Sy Brand's implementation of ranges::to in https://github.com/TartanLlama/ranges and neither of them compiles code converting a range to a std::map

我还没有尝试过 Sy 的实现,看起来 range-v3 的实现很奇怪:

#include <map>
#include <vector>
#include <range/v3/range/conversion.hpp>

int main() {
    std::vector<std::pair<int, int>> v = {{1, 2}, {3, 4}};

    // this works (explicit)
    // m1 is a std::map<int, int>
    auto m1 = ranges::to<std::map<int, int>>(v);

    // this works (deduced with a pipe)
    // m2 is a std::map<int, int>
    auto m2 = v | ranges::to<std::map>();

    // but this does not (deduced, direct call)
    auto m3 = ranges::to<std::map>(v);
}

问题是 range-v3 中的类模板直接调用版本出于某种原因专门尝试实例化 C<range_value_t<R>> (在这种情况下应该是 std::map<std::pair<int, int>>,显然是错误的)即使这里已经有一个元函数可以做正确的事情并且可以推断出 std::map<int, int>。 (由管道版本使用)。

ranges::to在标准库中指定这两个做同样(正确)的事情,所以这将在 C++23 中工作。这只是 range-v3 中的一个简单错误修复。

https://stackoverflow.com/questions/72082414/

相关文章:

haskell - 为什么不是 (20 >) 。长度 。取 10 === const True

awk - 用键分隔行并存储在不同的文件中

nuxt.js - 如何在 Nuxt 3 中间件获取当前域?

python - 如何找到具有最少步数的元素

r - 在嵌套列表中从第一个列表中选择第一个元素,从第二个列表中选择第二个元素,依此类推

ios - Xcode 构建失败 : Requested but did not find exte

module - 是否有一种简洁/内联的方式来创建 Set 值而不显式命名它们的类型?

r - 根据特定列中的数据框条目添加新列的最快方法是什么

r - 使用 Slice 或 Stringr 更改 R 中字符串向量中特定字符串的位置?

c++ - 用 CString 替换 LPCTSTR 是否安全?