Topic: two argument map insert (was: Isn't it time...)
Author: Matthew Woehlke <mw_triad@users.sourceforge.net>
Date: Thu, 01 Jan 2015 12:02:05 -0500
Raw View
On 2015-01-01 05:01, Andy Prowl wrote:
> On Tuesday, December 30, 2014 9:39:38 PM UTC+1, Matthew Woehlke wrote:
>> I didn't realize std::map *had* an emplace... it's a bit odd for
>> std::map, because you can't actually use it to construct the key or
>> value types, the way emplace works for e.g. std::vector.
>
> Actually, you can.
Not as easily / intuitively as with QMap / QHash:
struct Foo { Foo(int, double); };
QHash<int, Foo> hash;
hash.emplace(5, {1, 2});
I guess you mean that if I'd said '{5} instead of '5', it would work?
But that feels unnatural.
And... actually that doesn't work either. Please provide a working
example if you wish to convince me.
>> In fact, I fail to see why it's even variadic... how is
>> std::map::emplace anything but a shortcut for the appropriate
>> std::make_pair?
>
> I'm convinced the reason is to support the above-mentioned piecewise
> construction, which allows you holding non-movable types in a map.
How is the compiler supposed to figure out what arguments are used to
construct the key, and what arguments are used to construct the value?
What if I had something like this:
struct Foo { Foo(int); Foo(int, int); }
struct Bar { Bar(int); Bar(int, int); }
std::map<Foo, Bar> map;
How could I possibly call emplace and have the compiler know which
arguments are for which constructor? (Unless I am implicitly supplying
tuples, in which case, again, emplace should be taking exactly two
parameters.)
>> That said... no, it doesn't. At least on gcc 4.8.3, emplace isn't nearly
>> as useful as an insert/emplace taking the explicit argument types, as I
>> can't use brace initialization to construct the key/value.
>
> True, although if your types are movable, you can use insert() with
> brace-initialization [...]
So, to repeat the earlier question, why can't insert take a KeyType and
ValueType so that we don't need the outer braces? :-)
--
Matthew
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
.
Author: Andy Prowl <andy.prowl@gmail.com>
Date: Thu, 1 Jan 2015 09:22:20 -0800 (PST)
Raw View
------=_Part_171_297503135.1420132940063
Content-Type: multipart/alternative;
boundary="----=_Part_172_793259729.1420132940063"
------=_Part_172_793259729.1420132940063
Content-Type: text/plain; charset=UTF-8
On Thursday, January 1, 2015 6:02:22 PM UTC+1, Matthew Woehlke wrote:
>
>
> And... actually that doesn't work either. Please provide a working
> example if you wish to convince me. [...]
>
How is the compiler supposed to figure out what arguments are used to
> construct the key, and what arguments are used to construct the value?
>
> What if I had something like this:
>
> struct Foo { Foo(int); Foo(int, int); }
> struct Bar { Bar(int); Bar(int, int); }
> std::map<Foo, Bar> map;
>
> How could I possibly call emplace and have the compiler know which
> arguments are for which constructor? (Unless I am implicitly supplying
> tuples, in which case, again, emplace should be taking exactly two
> parameters.)
>
Indeed, as you wrote, you can provide two tuples. With reference to your
example:
struct Foo { Foo(int); Foo(int, int); }
struct Bar { Bar(int); Bar(int, int); }
std::map<Foo, Bar> map;
map.emplace(piecewise_construct, forward_as_tuple(42), forward_as_tuple(1337
, 1729));
You're right when you say this is not as simple/natural as in Qt, but you
*can* use emplace() to construct the key and the value type in-place.
Kind regards,
Andy
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_172_793259729.1420132940063
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">On Thursday, January 1, 2015 6:02:22 PM UTC+1, Matthew Woe=
hlke wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left=
: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><br>And... actually=
that doesn't work either. Please provide a working
<br>example if you wish to convince me. [...]<br></blockquote><blockquote c=
lass=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px=
#ccc solid;padding-left: 1ex;">How is the compiler supposed to figure out =
what arguments are used to
<br>construct the key, and what arguments are used to construct the value?
<br>
<br>What if I had something like this:
<br>
<br> struct Foo { Foo(int); Foo(int, int); }
<br> struct Bar { Bar(int); Bar(int, int); }
<br> std::map<Foo, Bar> map;
<br>
<br>How could I possibly call emplace and have the compiler know which
<br>arguments are for which constructor? (Unless I am implicitly supplying
<br>tuples, in which case, again, emplace should be taking exactly two
<br>parameters.)
<br></blockquote><div><br></div><div><span style=3D"font-size: 13.333333015=
4419px;">Indeed, as you wrote, you can provide two tuples. With reference t=
o your example:</span><br style=3D"font-size: 13.3333330154419px;"><br styl=
e=3D"font-size: 13.3333330154419px;"><div class=3D"prettyprint" style=3D"bo=
rder: 1px solid rgb(187, 187, 187); font-size: 13.3333330154419px; word-wra=
p: break-word; background-color: rgb(250, 250, 250);"><code class=3D"pretty=
print"><div class=3D"subprettyprint"><span style=3D"color: #008;" class=3D"=
styled-by-prettify">struct</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> </span><span style=3D"color: #606;" class=3D"styled-by-pre=
ttify">Foo</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">{</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span styl=
e=3D"color: #606;" class=3D"styled-by-prettify">Foo</span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #008=
;" class=3D"styled-by-prettify">int</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">);</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> </span><span style=3D"color: #606;" class=3D"styled-by-pr=
ettify">Foo</span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>(</span><span style=3D"color: #008;" class=3D"styled-by-prettify">int</spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">,</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"c=
olor: #008;" class=3D"styled-by-prettify">int</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">);</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">}</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"> <br></span><span style=3D"color: #008;" class=3D"styled-by-pretti=
fy">struct</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
</span><span style=3D"color: #606;" class=3D"styled-by-prettify">Bar</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">{</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #606=
;" class=3D"styled-by-prettify">Bar</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">(</span><span style=3D"color: #008;" class=3D"styl=
ed-by-prettify">int</span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">);</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
> </span><span style=3D"color: #606;" class=3D"styled-by-prettify">Bar</spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span s=
tyle=3D"color: #008;" class=3D"styled-by-prettify">int</span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">,</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" cla=
ss=3D"styled-by-prettify">int</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">);</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">}</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> <br>st=
d</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify">map</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify"><</span><span style=
=3D"color: #606;" class=3D"styled-by-prettify">Foo</span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;=
" class=3D"styled-by-prettify"> </span><span style=3D"color: #606;" class=
=3D"styled-by-prettify">Bar</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">></span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"> map</span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> <br=
><br>map</span><span style=3D"color: #660;" class=3D"styled-by-prettify">.<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify">emplace</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify">piecewise_construct</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">,</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify"> forward_as_tuple</span=
><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span st=
yle=3D"color: #066;" class=3D"styled-by-prettify">42</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">),</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"> forward_as_tuple</span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #066=
;" class=3D"styled-by-prettify">1337</span><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> </span><span style=3D"color: #066;" class=3D"styled-by-pr=
ettify">1729</span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">));</span></div><div><span class=3D"styled-by-prettify" style=3D"color: r=
gb(102, 102, 0);"><br></span></div></code></div></div><div> </div><div=
><span style=3D"font-size: 13.3333330154419px;">You're right when you say t=
his is not as simple/natural as in Qt, but you <i>can</i> use emplace() to =
construct the key and the value type in-place.</span><br></div><div><br></d=
iv><div>Kind regards,<br><br>Andy</div></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_172_793259729.1420132940063--
------=_Part_171_297503135.1420132940063--
.