c – std ::元组到元组和使用emplace的映射

c – std ::元组到元组和使用emplace的映射,第1张

概述请考虑以下代码,使用g 7.0.1(-std = c 17)编译: #include <map>#include <tuple>int main(){ // Create an alias for a tuple of three ints using ThreeTuple=std::tuple<int,int,int>; // Create an alias fo 请考虑以下代码,使用g 7.0.1(-std = c 17)编译:
#include <map>#include <tuple>int main(){    // Create an alias for a tuple of three ints    using ThreeTuple=std::tuple<int,int,int>;    // Create an alias for a map of tuple to tuple (of three ints)    using MapThreeTupletoThreeTuple=std::map<ThreeTuple,ThreeTuple>;    MapThreeTupletoThreeTuple m;    // The following does NOT compile    m.emplace({1,2,3},{4,5,6});    // ...,and neither does this    m.emplace(std::pIEcewise_construct,{1,6});}

我原以为map :: emplace()的initializer_List参数已经足够了,并且会导致将元组键插入指定的元组值关联.显然,编译器不同意.

当然创建一个元组显式(即,ThreeTuple {1,3}而不仅仅是{1,3})并将其传递给map :: emplace()解决了问题,但为什么不能初始化列表直接传递给map :: emplace(),它会自动将它们转发给元组构造函数?

解决方法

but why can’t the initializer Lists be passed directly to map::emplace()

因为初始化列表不是表达式,因此它们没有类型. emplace()的签名只是:

template< class... Args >std::pair<iterator,bool> emplace( Args&&... args );

而且你不能从{1,3}中推断出一种类型.你不能在C 11中,你仍然不能在C 1z.此规则的唯一例外是模板参数的格式为std :: initializer_List< T>其中T是模板参数.

为了m.emplace({1,6});工作,你需要一个签名,如:

std::pair<iterator,bool> emplace(key_type&&,mapped_type&&);
总结

以上是内存溢出为你收集整理的c – std ::元组到元组和使用emplace的映射全部内容,希望文章能够帮你解决c – std ::元组到元组和使用emplace的映射所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1241631.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存