为什么未为std :: multimap实现try_emplace?

时间:2018-12-14 01:05:59

标签: c++ c++17

C ++ 17为try_emplace引入了std::map方法,因此现在我可以编写如下代码:

struct Test
{
    Test(int i, int j){}
};
std::map<int, Test> tmap;
tmap.try_emplace(10, 10, 10);

但是try_emplace没有std::multimap<int, Test>,因此仍然需要piecewise_construct

这是否有技术原因?

2 个答案:

答案 0 :(得分:14)

  

是否有技术原因?

是的。 <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.9.0/slick.min.css"/> <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.9.0/slick-theme.css"/> <script type="text/javascript" src="//code.jquery.com/jquery-1.11.0.min.js"></script> <script type="text/javascript" src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.9.0/slick.min.js"></script> <div class="item__wrapper"> <div class="item active" id='item1'> <div class="item__text"> <div class="filter"> <h3>Some title 1</h3> <p>Efficiently communicate sticky quality vectors after compelling growth strategies. Compellingly scale future-proof content rather than enterprise users. Uniquely build scalable applications vis-a-vis performance based functionalities. Monotonectally procrastinate.</p> </div> </div> </div> <div class="item active" id="item2" > <div class="item__text"> <div class="filter"> <h3>Some title 2</h3> <p>Efficiently communicate sticky quality vectors after compelling growth strategies. Compellingly scale future-proof content rather than enterprise users. Uniquely build scalable applications vis-a-vis performance based functionalities. Monotonectally procrastinate.</p> </div> </div> </div> <div class="item active" id="item3" > <div class="item__text"> <div class="filter"> <h3>Some title 3</h3> <p>Efficiently communicate sticky quality vectors after compelling growth strategies. Compellingly scale future-proof content rather than enterprise users. Uniquely build scalable applications vis-a-vis performance based functionalities. Monotonectally procrastinate.</p> </div> </div> </div> </div> <div class="item__boxes"> <div class="box col-m-12 col-t-6 col-padding" data-slick-index="-1"> <div>Some Title</div> <div>Enthusiastically incubate diverse initiatives without multifunctional strategic theme areas.</div> </div> <div class="box col-m-12 col-t-6 col-padding" data-slick-index="0"> <div>Globally Title</div> <div>Globally impact integrated infomediaries via seamless mindshare. </div> </div> <div class="box col-m-12 col-t-6 col-padding" data-slick-index="1"> <div>Distinctively Title</div> <div>Distinctively strategize long-term high-impact ideas whereas client-centered leadership skills.</div> </div> </div>的目的是如果密钥已经存在于映射中则不执行任何操作。但是对于try_emplace(),每个键可以有多个值。的确,这些容器的要点是:给定键具有多个值。

结果,std::{unordered_,}multi{map,set}不会因为这些容器而失败-因此提供这样的功能会造成混乱和毫无意义。


根据评论,动机似乎只是try_emplace()的一部分,可以更轻松地实现价值。您可以为此编写一个辅助函数:

try_emplace()

哪怕是template <typename Map, typename Key, typename... Args> auto emplace_value(Map& map, Key&& key, Args&&... args) { return map.emplace(std::piecewise_construct, std::forward_as_tuple(std::forward<Key>(key)), std::forward_as_tuple(std::forward<Args>(args)...)); } ,您也可以写emplace_value(tmap, 10, 10, 10)

答案 1 :(得分:6)

这是不必要的,因为在多图情况下,因为没有唯一键,所以try_emplace永远不会失败。将try_emplace添加到映射的合理性是处理密钥已经存在的情况下所需的所有易于出错的代码,请参阅提案n4279强调我的):

  

唯一键映射容器的现有接口(std :: map,   std :: unordered_map)的设置稍有不足,因此确定   容器突变比起写起来更容易出错   。本文介绍了新的成员函数模板,以   填补这一空白。

     

新界面的理由和理由在   N3873。对以撒夸N3873的最初反应是   映射接口应该是固定的,而不是添加新的接口。我们   在Rapperswil的N4006中探索了这个想法,并决定   最初的建议是可取的(有一些名称更改)。这篇报告   仅汇总了建议的扩展名,而不重复原始的   讨论。我们仅在此处重述激励性代码段   动机:

std::map<std::string, std::unique_ptr<Foo>> m;
m["foo"] = nullptr;

auto ptr = std::make_unique_ptr<Foo>;
auto res = m.emplace("foo", std::move(ptr));

assert(ptr);    // ??? (may or may not fire)