STD :: ranges ::是否允许转换为STD :: MAP?
在std :: ranges :: to
paper wg21.link/p1206中,概述部分具有以下内容
//Supports converting associative container to sequence containers
auto f = ranges::to<vector>(m);
,但是我找不到转换为std :: map 在本文的其余部分中被载入。我尝试了range-v3和sy Brand的
范围:: to
in https:// https:// github.com/tartanllama/ranges ,它们都没有编译代码将范围转换为std :: map
。那么,这只是这些库中缺少的还是正在转换为std :: Map
不允许真正允许的?
In the std::ranges::to
paper wg21.link/p1206 ths overview section has the following
//Supports converting associative container to sequence containers
auto f = ranges::to<vector>(m);
However I can't find where the detail of converting to a std::map
is descibed in the rest of the paper. 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
. So is this just missing from those libraries or is converting to a std::map
not really intended to be allowed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。
的打破:
问题是,由于某种原因,范围V3中的类模板直接调用版本特别尝试实例化
c&lt; range_value_t&lt&lt&gt;&gt;&gt;
(这将是std :: map&lt; std :: pair&lt; int,int&gt;&gt;
在这种情况下,显然是错误的),即使这里已经有一个元功能可以做正确的事情,并且会推断出std :: map&lt; int,int&gt;
(由管道版本使用)。范围:: to
在标准库中指定了这两个执行相同(正确)的事情,因此这将在C ++ 23中起作用。这只是范围V3中的一个简单错误修复。Yes.
I haven't tried Sy's implementation, and it looks like range-v3's implementation is weirdly broken:
The issue is that the class template direct call version in range-v3 for some reason specifically tries to instantiate
C<range_value_t<R>>
(which would bestd::map<std::pair<int, int>>
in this case, clearly wrong) even though there is a metafunction here already that does the right thing and would deducestd::map<int, int>
(used by the pipe version).The
ranges::to
in the standard library specifies these two to do the same (correct) thing, so this will work in C++23. This is just an easy bug fix in range-v3.STD :: MAP to std :: vector:
根据 [range.utility.conv.to] ,这
将调用以下超载
其中
c
是vector
,r
ismap&lt; int,double&gt;&gt;
andargs。 ..
是空参数包。请注意,C ++ 23介绍以下 range range版本构造函数 for <代码>向量以及以下ctad
so
c(from_range,dectval&lt; r&gt;())
是一个有效的表达式,deduce_expr
的类型将为<代码> vector&lt; pair&lt; const int,double&gt; ,它将进一步调用,该>将使用
value_type
ofof
Pair&const构建
。vector
通过范围版本构造函数,int,double&gt;因此,
范围:: to&lt; vector&gt;(m)
在C ++ 23中基本上等同于range-v3失败是因为其内部实现检测
vector&lt; pair&lt; const int,double double&gt;
是可保留的,因此首先将默认默认构建vector
vector
和调用v.reserve()
预先分配内存,然后通过调用map
通过调用v.assign(),但由于
Pair&lt; const int,double&gt;
无法复制,因此编译失败。我怀疑这是范围V3的实现错误,因为如果
vector
's储备()
函数,则它将编译,并且此优化超负荷似乎并不限制value_type
必须复制
。
可 /code>还具有 sustructors 和对应的catd in在C ++ 23中,
因此遵循游戏的相同规则,我们将获得
map&lt; int,double&gt;
。std::map to std::vector:
According to the description of [range.utility.conv.to], this
will invoke the following overload
Where
C
isvector
,R
ismap<int, double>&
, andArgs...
is empty parameter pack. Note that C++23 introduces the following range version constructor forvector
and the following CTAD
So
C(from_range, declval<R>())
is a valid expression, and the type ofDEDUCE_EXPR
will bevector<pair<const int, double>>
, which will further invokewhich will construct a
vector
withvalue_type
ofpair<const int, double>
through the range version constructor.So
ranges::to<vector>(m)
in C++23 is basically equivalent toThe reason range-v3 fails is that its internal implementation detects that
vector<pair<const int, double>>
is reservable, so it will first default construct thevector
and callv.reserve()
to pre-allocate the memory, and then copy themap
by callingv.assign()
, but sincepair<const int, double>
is not copy assignable, so compilation fails.I suspect this is an implementation bug of range-v3, since it would compile if the
vector
'sreserve()
function didn't exist, and this optimized overload doesn't seem to constrain that thevalue_type
must be copy-assignable.std::vector to std::map:
And for the following
Since
std::map
also has the following constructors and corresponding CATD in in C++23So following the same rules of the game, we will get a
map<int, double>
.