Topic: idea: new overloadable operator: operator[]=


Author: xaxxon@gmail.com
Date: Mon, 31 Oct 2016 21:48:34 -0700 (PDT)
Raw View
------=_Part_566_380362290.1477975714683
Content-Type: multipart/alternative;
 boundary="----=_Part_567_1160951953.1477975714684"

------=_Part_567_1160951953.1477975714684
Content-Type: text/plain; charset=UTF-8

Was wondering if there had been any conversation about adding an
operator[]= which would be run in situations like

my_type_object["asdf"] = 4;



It would allow construction of an object with the value passed in instead
of default constructing an object and assigning or returning a proxy object
which isn't the intended type of a non-assigning call
my_type_object["asdf"].

If no operator[]= override is present on the type, the current behavior
would remain unchanged.

Among other places, this would remove the DefaultConstructible requirements
for std::map
operator[] http://en.cppreference.com/w/cpp/container/map/operator_at

This type of operation is present in other languages, such as javascript
proxy objects, which allow specific handling for setting attribute values
discretely from getting attribute values.
 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy


Any feedback greatly appreciated.

Thank you.

--Zac

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/5182c4e2-5fda-44bd-b473-6275d064d329%40isocpp.org.

------=_Part_567_1160951953.1477975714684
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Was wondering if there had been any conversation about add=
ing an operator[]=3D which would be run in situations like<div><br></div><d=
iv class=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250); bor=
der-color: rgb(187, 187, 187); border-style: solid; border-width: 1px; word=
-wrap: break-word;"><code class=3D"prettyprint"><div class=3D"subprettyprin=
t"><span style=3D"color: #000;" class=3D"styled-by-prettify">my_type_object=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">[</span><s=
pan style=3D"color: #080;" class=3D"styled-by-prettify">&quot;asdf&quot;</s=
pan><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: #660;" class=3D"styled-by-prettify">=3D</span><span style=3D"color:=
 #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #066;" c=
lass=3D"styled-by-prettify">4</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">;</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"><br><br></span></div></code></div><div><br><br></div><div>It woul=
d allow construction of an object with the value passed in instead of defau=
lt constructing an object and assigning or returning a proxy object which i=
sn&#39;t the intended type of a non-assigning call my_type_object[&quot;asd=
f&quot;]. =C2=A0=C2=A0</div><div><br></div><div>If no operator[]=3D overrid=
e is present on the type, the current behavior would remain unchanged.</div=
><div><br></div><div>Among other places, this would remove the DefaultConst=
ructible requirements for std::map operator[]=C2=A0http://en.cppreference.c=
om/w/cpp/container/map/operator_at</div><div><br></div><div>This type of op=
eration is present in other languages, such as javascript proxy objects, wh=
ich allow specific handling for setting attribute values discretely from ge=
tting attribute values. =C2=A0https://developer.mozilla.org/en-US/docs/Web/=
JavaScript/Reference/Global_Objects/Proxy</div><div><br></div><div><br></di=
v><div>Any feedback greatly appreciated.</div><div><br></div><div>Thank you=
..</div><div><br></div><div>--Zac</div></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/5182c4e2-5fda-44bd-b473-6275d064d329%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/5182c4e2-5fda-44bd-b473-6275d064d329=
%40isocpp.org</a>.<br />

------=_Part_567_1160951953.1477975714684--

------=_Part_566_380362290.1477975714683--

.


Author: "D. B." <db0451@gmail.com>
Date: Tue, 1 Nov 2016 07:35:29 +0000
Raw View
--001a114b6d886560a50540385ee0
Content-Type: text/plain; charset=UTF-8

Doesn't this required thatdifferent invocations of thing["repeatMe"] = would
act differently depending on whether the index had already been
constructed, at runtime? That seems like a lot of unintuitive ambiguity and
hassle for implementors to me.

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CACGiwhE_m28wrVe7s0bj0H%3DcoEuo6sNKekPDQf36MmG1JHq4VQ%40mail.gmail.com.

--001a114b6d886560a50540385ee0
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Doesn&#39;t this required thatdifferent invocations of <fo=
nt face=3D"monospace,monospace">thing[&quot;repeatMe&quot;] =3D <font face=
=3D"arial,helvetica,sans-serif">would act differently depending on whether =
the index had already been constructed, at runtime? That seems like a lot o=
f unintuitive ambiguity and hassle for implementors to me.</font></font><br=
></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CACGiwhE_m28wrVe7s0bj0H%3DcoEuo6sNKek=
PDQf36MmG1JHq4VQ%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">h=
ttps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CACGiwhE_m28wrV=
e7s0bj0H%3DcoEuo6sNKekPDQf36MmG1JHq4VQ%40mail.gmail.com</a>.<br />

--001a114b6d886560a50540385ee0--

.


Author: xaxxon@gmail.com
Date: Tue, 1 Nov 2016 01:42:59 -0700 (PDT)
Raw View
------=_Part_242_1147127050.1477989779535
Content-Type: multipart/alternative;
 boundary="----=_Part_243_1676496897.1477989779535"

------=_Part_243_1676496897.1477989779535
Content-Type: text/plain; charset=UTF-8

I was going to write some code to show how you were wrong and it wasn't
more complicated, but I kept running into weird corner cases.  Obviously I
should have done this before posting.

For example, an operator[] can't return a Value& unless a value is
guaranteed to exist (or it can throw an exception).   If it wouldn't be
created and you can't require exceptions (which seems a requirement), then
you'd have to return some sort of Optional<Value>, which is not ideal.

I'm open to any more feedback, but isn't sounding as great as it was
yesterday...

--Zac

On Tuesday, November 1, 2016 at 12:35:31 AM UTC-7, D. B. wrote:
>
> Doesn't this required thatdifferent invocations of thing["repeatMe"] = would
> act differently depending on whether the index had already been
> constructed, at runtime? That seems like a lot of unintuitive ambiguity and
> hassle for implementors to me.
>

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/6cb8b2ea-6e85-4ea5-82fc-7b8e763e0206%40isocpp.org.

------=_Part_243_1676496897.1477989779535
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">I was going to write some code to show how you were wrong =
and it wasn&#39;t more complicated, but I kept running into weird corner ca=
ses. =C2=A0Obviously I should have done this before posting. =C2=A0=C2=A0<d=
iv><br></div><div>For example, an operator[] can&#39;t return a Value&amp; =
unless a value is guaranteed to exist (or it can throw an exception). =C2=
=A0 If it wouldn&#39;t be created and you can&#39;t require exceptions (whi=
ch seems a requirement), then you&#39;d have to return some sort of Optiona=
l&lt;Value&gt;, which is not ideal. =C2=A0=C2=A0</div><div><br></div><div>I=
&#39;m open to any more feedback, but isn&#39;t sounding as great as it was=
 yesterday...</div><div><br></div><div>--Zac<br><br>On Tuesday, November 1,=
 2016 at 12:35:31 AM UTC-7, D. B. wrote:<blockquote class=3D"gmail_quote" s=
tyle=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-le=
ft: 1ex;"><div dir=3D"ltr">Doesn&#39;t this required thatdifferent invocati=
ons of <font face=3D"monospace,monospace">thing[&quot;repeatMe&quot;] =3D <=
font face=3D"arial,helvetica,sans-serif">would act differently depending on=
 whether the index had already been constructed, at runtime? That seems lik=
e a lot of unintuitive ambiguity and hassle for implementors to me.</font><=
/font><br></div>
</blockquote></div></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/6cb8b2ea-6e85-4ea5-82fc-7b8e763e0206%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/6cb8b2ea-6e85-4ea5-82fc-7b8e763e0206=
%40isocpp.org</a>.<br />

------=_Part_243_1676496897.1477989779535--

------=_Part_242_1147127050.1477989779535--

.


Author: mihailnajdenov@gmail.com
Date: Tue, 1 Nov 2016 01:44:45 -0700 (PDT)
Raw View
------=_Part_2488_2090005232.1477989885342
Content-Type: multipart/alternative;
 boundary="----=_Part_2489_1162624678.1477989885342"

------=_Part_2489_1162624678.1477989885342
Content-Type: text/plain; charset=UTF-8

This would mean, considering non-default constructible val

map["hi"] = val; //< compiles! (calls op []=)

val = map["hi"]; //< fails to compile! (calls op [])

This is very confusing.

Lets not forget the interface of map, as of c++17, is very, very
expressive.
emplace, try_emplace, insert, insert_or_assign, etc, etc - all work with
non-default constructible values.

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/01b58be9-31e1-4199-a8c9-3739046d2f83%40isocpp.org.

------=_Part_2489_1162624678.1477989885342
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>This would mean, considering non-default constructibl=
e val=C2=A0</div><div><br></div><code class=3D"prettyprint"><div><div class=
=3D"prettyprint" style=3D"border: 1px solid rgb(187, 187, 187); word-wrap: =
break-word; background-color: rgb(250, 250, 250);"><div class=3D"subprettyp=
rint"><span class=3D"styled-by-prettify" style=3D"color: #000;">map</span><=
span class=3D"styled-by-prettify" style=3D"color: #660;">[</span><span clas=
s=3D"styled-by-prettify" style=3D"color: #080;">&quot;hi&quot;</span><span =
class=3D"styled-by-prettify" style=3D"color: #660;">]</span><span class=3D"=
styled-by-prettify" style=3D"color: #000;"> </span><span class=3D"styled-by=
-prettify" style=3D"color: #660;">=3D</span><span class=3D"styled-by-pretti=
fy" style=3D"color: #000;"> val</span><span class=3D"styled-by-prettify" st=
yle=3D"color: #660;">;</span><span class=3D"styled-by-prettify" style=3D"co=
lor: #000;"> </span><span class=3D"styled-by-prettify" style=3D"color: #800=
;">//&lt; compiles!=C2=A0(calls op []=3D)<br><br><span class=3D"styled-by-p=
rettify" style=3D"color: #000;">val </span><span class=3D"styled-by-prettif=
y" style=3D"color: #660;">=3D</span><span class=3D"styled-by-prettify" styl=
e=3D"color: #000;"> map</span><span class=3D"styled-by-prettify" style=3D"c=
olor: #660;">[</span><span class=3D"styled-by-prettify" style=3D"color: #08=
0;">&quot;hi&quot;</span><span class=3D"styled-by-prettify" style=3D"color:=
 #660;">];</span><span class=3D"styled-by-prettify" style=3D"color: #000;">=
 </span><span class=3D"styled-by-prettify" style=3D"color: #800;">//&lt; fa=
ils to</span><span class=3D"styled-by-prettify" style=3D"color: #800;"> com=
pile! (calls op [])</span><span class=3D"styled-by-prettify" style=3D"color=
: #000;"></span></span><span class=3D"styled-by-prettify" style=3D"color: #=
000;"><b></b><i></i><u></u><sub></sub><sup></sup><strike></strike><br></spa=
n></div></div></div></code><div class=3D"prettyprint" style=3D"border: 1px =
solid rgb(187, 187, 187); word-wrap: break-word; background-color: rgb(250,=
 250, 250);"></div><div><br></div><div>This is very confusing. </div><div><=
br></div><div>Lets not forget the interface of map, as of c++17, is very, v=
ery expressive. </div><div>emplace, try_emplace, insert,=C2=A0insert_or_ass=
ign, etc, etc - all work with non-default constructible values. <br></div><=
/div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/01b58be9-31e1-4199-a8c9-3739046d2f83%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/01b58be9-31e1-4199-a8c9-3739046d2f83=
%40isocpp.org</a>.<br />

------=_Part_2489_1162624678.1477989885342--

------=_Part_2488_2090005232.1477989885342--

.


Author: "D. B." <db0451@gmail.com>
Date: Tue, 1 Nov 2016 08:57:13 +0000
Raw View
--001a113677f4b4d7580540398274
Content-Type: text/plain; charset=UTF-8

On Tue, Nov 1, 2016 at 8:42 AM, <xaxxon@gmail.com> wrote:

> I was going to write some code to show how you were wrong and it wasn't
> more complicated, but I kept running into weird corner cases.  Obviously I
> should have done this before posting.
>
> For example, an operator[] can't return a Value& unless a value is
> guaranteed to exist (or it can throw an exception).   If it wouldn't be
> created and you can't require exceptions (which seems a requirement), then
> you'd have to return some sort of Optional<Value>, which is not ideal.
>
> I'm open to any more feedback, but isn't sounding as great as it was
> yesterday...
>
>
Heh. I guess my overall view on it is - assuming it can be done somehow -
would the likely complexity for users and implementors outweigh the cost of
just either

   - setting things up so the type is default-constructible, which is OK if
   the proxy is cheap, as they should be, or
   - using a dedicated function that clearly signals its intent, such as
   try_emplace, instead of trying to bend an operator into its role.

Operators IMO should be simple and unsurprising, due to following
intended/conventional patterns, but it seems to me that operator[]= would
be confusing *by convention*.

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CACGiwhFqdUiWKgB9bNrC9qEOHWGRW2q6BjyR_3Tr3DPOseY_cQ%40mail.gmail.com.

--001a113677f4b4d7580540398274
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"gmail_quote">On T=
ue, Nov 1, 2016 at 8:42 AM,  <span dir=3D"ltr">&lt;<a href=3D"mailto:xaxxon=
@gmail.com" target=3D"_blank">xaxxon@gmail.com</a>&gt;</span> wrote:<br><bl=
ockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #=
ccc solid;padding-left:1ex"><div dir=3D"ltr">I was going to write some code=
 to show how you were wrong and it wasn&#39;t more complicated, but I kept =
running into weird corner cases.=C2=A0 Obviously I should have done this be=
fore posting. =C2=A0=C2=A0<div><br></div><div>For example, an operator[] ca=
n&#39;t return a Value&amp; unless a value is guaranteed to exist (or it ca=
n throw an exception). =C2=A0 If it wouldn&#39;t be created and you can&#39=
;t require exceptions (which seems a requirement), then you&#39;d have to r=
eturn some sort of Optional&lt;Value&gt;, which is not ideal. =C2=A0=C2=A0<=
/div><div><br></div><div>I&#39;m open to any more feedback, but isn&#39;t s=
ounding as great as it was yesterday...<br><br></div></div></blockquote><di=
v><br></div><div>Heh. I guess my overall view on it is - assuming it can be=
 done somehow - would the likely complexity for users and implementors outw=
eigh the cost of just either<br><ul><li>setting things up so the type is de=
fault-constructible, which is OK if the proxy is cheap, as they should be, =
or</li><li>using a dedicated function that clearly signals its intent, such=
 as try_emplace, instead of trying to bend an operator into its role.</li><=
/ul>Operators IMO should be simple and unsurprising, due to following inten=
ded/conventional patterns, but it seems to me that <span style=3D"font-fami=
ly:monospace,monospace">operator[]=3D</span> would be confusing <i>by conve=
ntion</i>.</div></div><br></div></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CACGiwhFqdUiWKgB9bNrC9qEOHWGRW2q6BjyR=
_3Tr3DPOseY_cQ%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">htt=
ps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CACGiwhFqdUiWKgB9=
bNrC9qEOHWGRW2q6BjyR_3Tr3DPOseY_cQ%40mail.gmail.com</a>.<br />

--001a113677f4b4d7580540398274--

.


Author: btcrtn@gmail.com
Date: Tue, 1 Nov 2016 01:57:21 -0700 (PDT)
Raw View
------=_Part_991_1695107355.1477990641182
Content-Type: multipart/alternative;
 boundary="----=_Part_992_748293418.1477990641182"

------=_Part_992_748293418.1477990641182
Content-Type: text/plain; charset=UTF-8

If we want to optimize for this case, we can write a member function
(simplified)

IterT insert(KeyT key, ValueT value)

instead of inventing a new operator and the effort that goes with it.


Requiring DefaultConstructible could, I think, be solved by using a special
kind of proxy.
(But it will break cases like this)
map["some_key"];

struct UninitializedProxy
{
/*
  If map[key] exists, call UninitializedProxy(nullptr, p) where p is the
existing object.
  Else, call UninitializedProxy(q, nullptr) where q is the uninitialized
block of memory.
*/
  UninitializedProxy(void* uninitialized_mem, ValueT* existing_obj)
    : mem(uninitialized_mem)
    , obj(existing_obj)
  {}

  ~UninitializedProxy() {
    if (!obj) {
      // Was not initialized, remove from the container.
    }
  }

  UninitializedProxy& operator = (ValueT value) {
    if (obj) {
      *obj = value;
    }
    else {
      obj = new(mem) ValueT(value);
    }
    return *this;
  }

  void* mem;
  ValueT* obj;
};


--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/e70f5b39-790b-4296-913f-3fd303622dde%40isocpp.org.

------=_Part_992_748293418.1477990641182
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">If we want to optimize for this case, we can write a membe=
r function (simplified)<br><br><div style=3D"background-color: rgb(250, 250=
, 250); border-color: rgb(187, 187, 187); border-style: solid; border-width=
: 1px; overflow-wrap: break-word;" class=3D"prettyprint"><code class=3D"pre=
ttyprint"><div class=3D"subprettyprint"><span style=3D"color: #606;" class=
=3D"styled-by-prettify">IterT</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"> insert</span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">(</span><span style=3D"color: #606;" class=3D"styled-by-pre=
ttify">KeyT</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
> key</span><span style=3D"color: #660;" class=3D"styled-by-prettify">,</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span =
style=3D"color: #606;" class=3D"styled-by-prettify">ValueT</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"> value</span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">)</span></div></code></div><br>=
instead of inventing a new operator and the effort that goes with it.<br><b=
r><br>Requiring DefaultConstructible could, I think, be solved by using a s=
pecial kind of proxy.<br>(But it will break cases like this)<br><div style=
=3D"background-color: rgb(250, 250, 250); border-color: rgb(187, 187, 187);=
 border-style: solid; border-width: 1px; overflow-wrap: break-word;" class=
=3D"prettyprint"><code class=3D"prettyprint"><div class=3D"subprettyprint">=
<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"c=
olor: #080;" class=3D"styled-by-prettify">&quot;some_key&quot;</span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">];</span></div></code><=
/div><br><div style=3D"background-color: rgb(250, 250, 250); border-color: =
rgb(187, 187, 187); border-style: solid; border-width: 1px; overflow-wrap: =
break-word;" class=3D"prettyprint"><code class=3D"prettyprint"><div class=
=3D"subprettyprint"><span style=3D"color: #008;" class=3D"styled-by-prettif=
y">struct</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> =
</span><span style=3D"color: #606;" class=3D"styled-by-prettify">Uninitiali=
zedProxy</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><b=
r></span><span style=3D"color: #660;" class=3D"styled-by-prettify">{</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span =
style=3D"color: #800;" class=3D"styled-by-prettify">/*<br>=C2=A0 If map[key=
] exists, call UninitializedProxy(nullptr, p) where p is the existing objec=
t.<br>=C2=A0 Else, call UninitializedProxy(q, nullptr) where q is the unini=
tialized block of memory.<br>*/</span><span style=3D"color: #000;" class=3D=
"styled-by-prettify"><br>=C2=A0 </span><span style=3D"color: #606;" class=
=3D"styled-by-prettify">UninitializedProxy</span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">(</span><span style=3D"color: #008;" class=
=3D"styled-by-prettify">void</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">*</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"> uninitialized_mem</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"> </span><span style=3D"color: #606;" class=3D"styled-by-prettify">V=
alueT</span><span style=3D"color: #660;" class=3D"styled-by-prettify">*</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> existing_obj<=
/span><span style=3D"color: #660;" class=3D"styled-by-prettify">)</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 =C2=A0 </=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">:</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"> mem</span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify">uninitialized_mem</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">)</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"><br>=C2=A0 =C2=A0 </span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"> obj</span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">existing_obj</span><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify">)</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"><br>=C2=A0 </span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">{}</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"><br><br>=C2=A0 </span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">~</span><span style=3D"color: #606;" class=3D"styled-by-pret=
tify">UninitializedProxy</span><span style=3D"color: #660;" class=3D"styled=
-by-prettify">()</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">{</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 =
=C2=A0 </span><span style=3D"color: #008;" class=3D"styled-by-prettify">if<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">(!</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">obj</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: #660;" class=
=3D"styled-by-prettify">{</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"><br>=C2=A0 =C2=A0 =C2=A0 </span><span style=3D"color: #800;"=
 class=3D"styled-by-prettify">// Was not initialized, remove from the conta=
iner.</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>=
=C2=A0 =C2=A0 </span><span style=3D"color: #660;" class=3D"styled-by-pretti=
fy">}</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>=
=C2=A0 </span><span style=3D"color: #660;" class=3D"styled-by-prettify">}</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br><br>=C2=
=A0 </span><span style=3D"color: #606;" class=3D"styled-by-prettify">Uninit=
ializedProxy</span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">&amp;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </=
span><span style=3D"color: #008;" class=3D"styled-by-prettify">operator</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">=3D</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: #606;" =
class=3D"styled-by-prettify">ValueT</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"> value</span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">)</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>=
=C2=A0 =C2=A0 </span><span style=3D"color: #008;" class=3D"styled-by-pretti=
fy">if</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </s=
pan><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span=
 style=3D"color: #000;" class=3D"styled-by-prettify">obj</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: #660;" =
class=3D"styled-by-prettify">{</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"><br>=C2=A0 =C2=A0 =C2=A0 </span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">*</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify">obj </span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">=3D</span><span style=3D"color: #000;" class=3D"styled-=
by-prettify"> value</span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">;</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
<br>=C2=A0 =C2=A0 </span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">}</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><=
br>=C2=A0 =C2=A0 </span><span style=3D"color: #008;" class=3D"styled-by-pre=
ttify">else</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>=C2=A0 =C2=A0=
 =C2=A0 obj </span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">=3D</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </sp=
an><span style=3D"color: #008;" class=3D"styled-by-prettify">new</span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">mem</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">ValueT</span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify">value</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">);</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><b=
r>=C2=A0 =C2=A0 </span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">}</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br=
>=C2=A0 =C2=A0 </span><span style=3D"color: #008;" class=3D"styled-by-prett=
ify">return</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: #008;" class=3D"styled-by-prettify">this</span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">;</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 </span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">}</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify"><br><br>=C2=A0 </span><span style=3D"col=
or: #008;" class=3D"styled-by-prettify">void</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">*</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"> mem</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">;</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"><br>=C2=A0 </span><span style=3D"color: #606;" class=3D"styled-by=
-prettify">ValueT</span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">*</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> o=
bj</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">};</span></div></code><=
/div><br><br></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/e70f5b39-790b-4296-913f-3fd303622dde%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/e70f5b39-790b-4296-913f-3fd303622dde=
%40isocpp.org</a>.<br />

------=_Part_992_748293418.1477990641182--

------=_Part_991_1695107355.1477990641182--

.


Author: Andrey Semashev <andrey.semashev@gmail.com>
Date: Tue, 1 Nov 2016 12:24:29 +0300
Raw View
On 11/01/16 07:48, xaxxon@gmail.com wrote:
> Was wondering if there had been any conversation about adding an
> operator[]= which would be run in situations like
>
> |
> my_type_object["asdf"]=4;
>
> |
>
>
> It would allow construction of an object with the value passed in
> instead of default constructing an object and assigning or returning a
> proxy object which isn't the intended type of a non-assigning call
> my_type_object["asdf"].
>
> If no operator[]= override is present on the type, the current behavior
> would remain unchanged.
>
> Among other places, this would remove the DefaultConstructible
> requirements for std::map
> operator[] http://en.cppreference.com/w/cpp/container/map/operator_at
>
> This type of operation is present in other languages, such as javascript
> proxy objects, which allow specific handling for setting attribute
> values discretely from getting attribute values.
>  https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
>
>
> Any feedback greatly appreciated.

I think, a theoretical operator[]= would be very confusing, since it
would merge two distinct operations into one. It would certainly break a
lot of code that currently only defines operator[] and operator=.

Fixing map/unordered_map operator[] instead is a less radical and
confusing approach. Let it return a proxy, which will either read or
insert/assign the mapped value depending on the context. Such a change
is local to the library and does not require a core language modification.


--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/cd358b26-8ae0-c8d5-30a4-5aefc949cc44%40gmail.com.

.


Author: Zac Hansen <xaxxon@gmail.com>
Date: Tue, 1 Nov 2016 02:27:03 -0700
Raw View
--001a113cf110707bbb054039ed5c
Content-Type: text/plain; charset=UTF-8

My thought was if you didn't overload operator[]= it would continue the
existing behavior, so it wouldn't break any code.   Also, I didn't actually
want this for fixing std::map - it came up in some code I was writing to
interface with javascript where I had to write a proxy object because I
cannot create an uninitialized object even for a short period of time.

On Tue, Nov 1, 2016 at 2:24 AM, Andrey Semashev <andrey.semashev@gmail.com>
wrote:

> On 11/01/16 07:48, xaxxon@gmail.com wrote:
>
>> Was wondering if there had been any conversation about adding an
>> operator[]= which would be run in situations like
>>
>> |
>> my_type_object["asdf"]=4;
>>
>> |
>>
>>
>> It would allow construction of an object with the value passed in
>> instead of default constructing an object and assigning or returning a
>> proxy object which isn't the intended type of a non-assigning call
>> my_type_object["asdf"].
>>
>> If no operator[]= override is present on the type, the current behavior
>> would remain unchanged.
>>
>> Among other places, this would remove the DefaultConstructible
>> requirements for std::map
>> operator[] http://en.cppreference.com/w/cpp/container/map/operator_at
>>
>> This type of operation is present in other languages, such as javascript
>> proxy objects, which allow specific handling for setting attribute
>> values discretely from getting attribute values.
>>  https://developer.mozilla.org/en-US/docs/Web/JavaScript/
>> Reference/Global_Objects/Proxy
>>
>>
>> Any feedback greatly appreciated.
>>
>
> I think, a theoretical operator[]= would be very confusing, since it would
> merge two distinct operations into one. It would certainly break a lot of
> code that currently only defines operator[] and operator=.
>
> Fixing map/unordered_map operator[] instead is a less radical and
> confusing approach. Let it return a proxy, which will either read or
> insert/assign the mapped value depending on the context. Such a change is
> local to the library and does not require a core language modification.
>
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "ISO C++ Standard - Future Proposals" group.
> To unsubscribe from this topic, visit https://groups.google.com/a/is
> ocpp.org/d/topic/std-proposals/09a4G1aXF8U/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> std-proposals+unsubscribe@isocpp.org.
> To post to this group, send email to std-proposals@isocpp.org.
> To view this discussion on the web visit https://groups.google.com/a/is
> ocpp.org/d/msgid/std-proposals/cd358b26-8ae0-c8d5-30a4-
> 5aefc949cc44%40gmail.com.
>

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CABi3GVDJp7%2Bx1-Q2vtY-%2BR7KOwtpC29ME3JMM_yYp2E-JvPQ_A%40mail.gmail.com.

--001a113cf110707bbb054039ed5c
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">My thought was if you didn&#39;t overload operator[]=3D it=
 would continue the existing behavior, so it wouldn&#39;t break any code. =
=C2=A0 Also, I didn&#39;t actually want this for fixing std::map - it came =
up in some code I was writing to interface with javascript where I had to w=
rite a proxy object because I cannot create an uninitialized object even fo=
r a short period of time.</div><div class=3D"gmail_extra"><br><div class=3D=
"gmail_quote">On Tue, Nov 1, 2016 at 2:24 AM, Andrey Semashev <span dir=3D"=
ltr">&lt;<a href=3D"mailto:andrey.semashev@gmail.com" target=3D"_blank">and=
rey.semashev@gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_=
quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1=
ex"><span class=3D"">On 11/01/16 07:48, <a href=3D"mailto:xaxxon@gmail.com"=
 target=3D"_blank">xaxxon@gmail.com</a> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">
Was wondering if there had been any conversation about adding an<br>
operator[]=3D which would be run in situations like<br>
<br>
|<br>
my_type_object[&quot;asdf&quot;]=3D4;<br>
<br>
|<br>
<br>
<br>
It would allow construction of an object with the value passed in<br>
instead of default constructing an object and assigning or returning a<br>
proxy object which isn&#39;t the intended type of a non-assigning call<br>
my_type_object[&quot;asdf&quot;].<br>
<br>
If no operator[]=3D override is present on the type, the current behavior<b=
r>
would remain unchanged.<br>
<br>
Among other places, this would remove the DefaultConstructible<br>
requirements for std::map<br>
operator[] <a href=3D"http://en.cppreference.com/w/cpp/container/map/operat=
or_at" rel=3D"noreferrer" target=3D"_blank">http://en.cppreference.com/w/c<=
wbr>pp/container/map/operator_at</a><br>
<br>
This type of operation is present in other languages, such as javascript<br=
>
proxy objects, which allow specific handling for setting attribute<br>
values discretely from getting attribute values.<br>
=C2=A0<a href=3D"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re=
ference/Global_Objects/Proxy" rel=3D"noreferrer" target=3D"_blank">https://=
developer.mozilla.<wbr>org/en-US/docs/Web/JavaScript/<wbr>Reference/Global_=
Objects/Proxy</a><br>
<br>
<br>
Any feedback greatly appreciated.<br>
</blockquote>
<br></span>
I think, a theoretical operator[]=3D would be very confusing, since it woul=
d merge two distinct operations into one. It would certainly break a lot of=
 code that currently only defines operator[] and operator=3D.<br>
<br>
Fixing map/unordered_map operator[] instead is a less radical and confusing=
 approach. Let it return a proxy, which will either read or insert/assign t=
he mapped value depending on the context. Such a change is local to the lib=
rary and does not require a core language modification.<span class=3D""><br=
>
<br>
<br>
-- <br>
You received this message because you are subscribed to a topic in the Goog=
le Groups &quot;ISO C++ Standard - Future Proposals&quot; group.<br>
To unsubscribe from this topic, visit <a href=3D"https://groups.google.com/=
a/isocpp.org/d/topic/std-proposals/09a4G1aXF8U/unsubscribe" rel=3D"noreferr=
er" target=3D"_blank">https://groups.google.com/a/is<wbr>ocpp.org/d/topic/s=
td-proposals<wbr>/09a4G1aXF8U/unsubscribe</a>.<br>
To unsubscribe from this group and all its topics, send an email to <a href=
=3D"mailto:std-proposals%2Bunsubscribe@isocpp.org" target=3D"_blank">std-pr=
oposals+unsubscribe@isoc<wbr>pp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br></span>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/cd358b26-8ae0-c8d5-30a4-5aefc949cc44%=
40gmail.com" rel=3D"noreferrer" target=3D"_blank">https://groups.google.com=
/a/is<wbr>ocpp.org/d/msgid/std-proposals<wbr>/cd358b26-8ae0-c8d5-30a4-<wbr>=
5aefc949cc44%40gmail.com</a>.<br>
</blockquote></div><br></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CABi3GVDJp7%2Bx1-Q2vtY-%2BR7KOwtpC29M=
E3JMM_yYp2E-JvPQ_A%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter"=
>https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CABi3GVDJp7%2=
Bx1-Q2vtY-%2BR7KOwtpC29ME3JMM_yYp2E-JvPQ_A%40mail.gmail.com</a>.<br />

--001a113cf110707bbb054039ed5c--

.


Author: Andrey Semashev <andrey.semashev@gmail.com>
Date: Tue, 1 Nov 2016 12:39:46 +0300
Raw View
On 11/01/16 12:27, Zac Hansen wrote:
> My thought was if you didn't overload operator[]= it would continue the
> existing behavior, so it wouldn't break any code.   Also, I didn't
> actually want this for fixing std::map - it came up in some code I was
> writing to interface with javascript where I had to write a proxy object
> because I cannot create an uninitialized object even for a short period
> of time.

Again, this is possible with a library-only solution.

> On Tue, Nov 1, 2016 at 2:24 AM, Andrey Semashev
> <andrey.semashev@gmail.com <mailto:andrey.semashev@gmail.com>> wrote:
>
>     On 11/01/16 07:48, xaxxon@gmail.com <mailto:xaxxon@gmail.com> wrote:
>
>         Was wondering if there had been any conversation about adding an
>         operator[]= which would be run in situations like
>
>         |
>         my_type_object["asdf"]=4;
>
>         |
>
>
>         It would allow construction of an object with the value passed in
>         instead of default constructing an object and assigning or
>         returning a
>         proxy object which isn't the intended type of a non-assigning call
>         my_type_object["asdf"].
>
>         If no operator[]= override is present on the type, the current
>         behavior
>         would remain unchanged.
>
>         Among other places, this would remove the DefaultConstructible
>         requirements for std::map
>         operator[]
>         http://en.cppreference.com/w/cpp/container/map/operator_at
>         <http://en.cppreference.com/w/cpp/container/map/operator_at>
>
>         This type of operation is present in other languages, such as
>         javascript
>         proxy objects, which allow specific handling for setting attribute
>         values discretely from getting attribute values.
>          https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
>         <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy>
>
>
>         Any feedback greatly appreciated.
>
>
>     I think, a theoretical operator[]= would be very confusing, since it
>     would merge two distinct operations into one. It would certainly
>     break a lot of code that currently only defines operator[] and
>     operator=.
>
>     Fixing map/unordered_map operator[] instead is a less radical and
>     confusing approach. Let it return a proxy, which will either read or
>     insert/assign the mapped value depending on the context. Such a
>     change is local to the library and does not require a core language
>     modification.


--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/283d1b64-aee9-29d7-ed56-064dec97c3fd%40gmail.com.

.


Author: "D. B." <db0451@gmail.com>
Date: Tue, 1 Nov 2016 09:46:04 +0000
Raw View
--001a1130cf06707bf305403a31d1
Content-Type: text/plain; charset=UTF-8

On Tue, Nov 1, 2016 at 9:24 AM, Andrey Semashev <andrey.semashev@gmail.com>
wrote:

>
> I think, a theoretical operator[]= would be very confusing, since it would
> merge two distinct operations into one. It would certainly break a lot of
> code that currently only defines operator[] and operator=.
>

Agreed,


>
> Fixing map/unordered_map operator[] instead is a less radical and
> confusing approach. Let it return a proxy, which will either read or
> insert/assign the mapped value depending on the context. Such a change is
> local to the library and does not require a core language modification.
>
>
This is basically what the maps already do. I think the OP's point is that
they want to use *construction*, not assignment, if the index does not yet
exist. That would remove the requirement for the mapped (or any other
contained) type to be default-constructible.

But yes, requiring a new operator - which in itself is a conflation of 2
existing ones - seems like a fraught way to do this.

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CACGiwhET5-9f15kJyiW0hWjdFWD-uwRRzLmdygEpGXS%2BndhXJw%40mail.gmail.com.

--001a1130cf06707bf305403a31d1
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"gmail_extra"><br><div class=3D"gmail_quote">=
On Tue, Nov 1, 2016 at 9:24 AM, Andrey Semashev <span dir=3D"ltr">&lt;<a hr=
ef=3D"mailto:andrey.semashev@gmail.com" target=3D"_blank">andrey.semashev@g=
mail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=
=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span cl=
ass=3D""><br></span>
I think, a theoretical operator[]=3D would be very confusing, since it woul=
d merge two distinct operations into one. It would certainly break a lot of=
 code that currently only defines operator[] and operator=3D.<br></blockquo=
te><div><br></div><div>Agreed,<br></div><div>=C2=A0</div><blockquote class=
=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padd=
ing-left:1ex">
<br>
Fixing map/unordered_map operator[] instead is a less radical and confusing=
 approach. Let it return a proxy, which will either read or insert/assign t=
he mapped value depending on the context. Such a change is local to the lib=
rary and does not require a core language modification.<span class=3D""><br=
>
<br></span></blockquote><div><br></div><div>This is basically what the maps=
 already do. I think the OP&#39;s point is that they want to use <i>constru=
ction</i>, not assignment, if the index does not yet exist. That would remo=
ve the requirement for the mapped (or any other contained) type to be defau=
lt-constructible.<br><br></div><div>But yes, requiring a new operator - whi=
ch in itself is a conflation of 2 existing ones - seems like a fraught way =
to do this.<br></div><div>=C2=A0<br></div></div><br></div></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CACGiwhET5-9f15kJyiW0hWjdFWD-uwRRzLmd=
ygEpGXS%2BndhXJw%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">h=
ttps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CACGiwhET5-9f15=
kJyiW0hWjdFWD-uwRRzLmdygEpGXS%2BndhXJw%40mail.gmail.com</a>.<br />

--001a1130cf06707bf305403a31d1--

.


Author: Andrey Semashev <andrey.semashev@gmail.com>
Date: Tue, 1 Nov 2016 12:58:11 +0300
Raw View
On 11/01/16 12:46, D. B. wrote:
>
> On Tue, Nov 1, 2016 at 9:24 AM, Andrey Semashev
> <andrey.semashev@gmail.com <mailto:andrey.semashev@gmail.com>> wrote:
>
>     Fixing map/unordered_map operator[] instead is a less radical and
>     confusing approach. Let it return a proxy, which will either read or
>     insert/assign the mapped value depending on the context. Such a
>     change is local to the library and does not require a core language
>     modification.
>
> This is basically what the maps already do.

I don't think so. According to the standard, opereator[] returns a
reference to the mapped type, which implies the operator has to
construct the element if it's not found. I was suggesting letting it
return an unspecified proxy object, which would have an overloaded
operator= and operator T - the former would perform an insertion or
assignment to the mapped object, the latter would perform a lookup or
default-construction.

In case of std::map, the default-construction behavior maintains
backward compatibility with the current behavior. In user's code (i.e.
in classes other than std::map & friends) this behavior could be different.

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/7e5b2b22-79d3-0a77-5dd2-f84848bbbe7d%40gmail.com.

.


Author: Nicol Bolas <jmckesson@gmail.com>
Date: Tue, 1 Nov 2016 07:16:42 -0700 (PDT)
Raw View
------=_Part_2276_874719072.1478009803061
Content-Type: multipart/alternative;
 boundary="----=_Part_2277_260833761.1478009803062"

------=_Part_2277_260833761.1478009803062
Content-Type: text/plain; charset=UTF-8

On Tuesday, November 1, 2016 at 5:58:14 AM UTC-4, Andrey Semashev wrote:
>
> On 11/01/16 12:46, D. B. wrote:
> >
> > On Tue, Nov 1, 2016 at 9:24 AM, Andrey Semashev
> > <andrey....@gmail.com <javascript:> <mailto:andrey....@gmail.com
> <javascript:>>> wrote:
> >
> >     Fixing map/unordered_map operator[] instead is a less radical and
> >     confusing approach. Let it return a proxy, which will either read or
> >     insert/assign the mapped value depending on the context. Such a
> >     change is local to the library and does not require a core language
> >     modification.
> >
> > This is basically what the maps already do.
>
> I don't think so. According to the standard, opereator[] returns a
> reference to the mapped type, which implies the operator has to
> construct the element if it's not found. I was suggesting letting it
> return an unspecified proxy object, which would have an overloaded
> operator= and operator T - the former would perform an insertion or
> assignment to the mapped object, the latter would perform a lookup or
> default-construction.
>
> In case of std::map, the default-construction behavior maintains
> backward compatibility with the current behavior. In user's code (i.e.
> in classes other than std::map & friends) this behavior could be
> different.
>
>
The problem with returning proxy objects is, as with many things, `auto`.

Let's say you have a `map<string, T>`. If you do something like `m["foo"] =
data;`, that works. It works because `std::string` is implicitly
convertible from a string literal.

If you change `map::operator[]` to return a proxy (ignoring backwards
compatibility issues here). The idea is that the proxy will effectively
call `insert_or_assign` with the value you give it. And it is implicitly
convertible to the value, default constructing it if it doesn't currently
exists.

But to do that, the proxy object needs two things: a reference to the `map`
that created it, and... the key the proxy is for. Is it going to store that
key as a reference? If so, then what happens when the key is a temporary,
like with `m["foo"]`?

So if you do:

auto proxy = m["foo"];
proxy = T{};

Then `proxy` will have a reference to a destroyed temporary. You could have
`proxy` store the key by value, but that makes `proxy` rather large.

It would also mean that you couldn't do `auto &t = m["foo"];`, since the
return value would undoubtedly be a prvalue. That makes the `operator[]`
interface a lot less user-friendly.

Right now, C++ has too many problems with proxy objects to be able to use
them in the way you're trying to use them. What you really want is for
`auto proxy = m["foo"]` to deduce the `value_type`, not the proxy object's
type. And right now, we don't have a way to make that work.

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/8dab5ade-7159-4aa2-976a-c0086a904656%40isocpp.org.

------=_Part_2277_260833761.1478009803062
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">On Tuesday, November 1, 2016 at 5:58:14 AM UTC-4, Andrey S=
emashev wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-l=
eft: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">On 11/01/16 12:4=
6, D. B. wrote:
<br>&gt;
<br>&gt; On Tue, Nov 1, 2016 at 9:24 AM, Andrey Semashev
<br>&gt; &lt;<a href=3D"javascript:" target=3D"_blank" gdf-obfuscated-mailt=
o=3D"xZ5U_9OhBAAJ" rel=3D"nofollow" onmousedown=3D"this.href=3D&#39;javascr=
ipt:&#39;;return true;" onclick=3D"this.href=3D&#39;javascript:&#39;;return=
 true;">andrey....@gmail.com</a> &lt;mailto:<a href=3D"javascript:" target=
=3D"_blank" gdf-obfuscated-mailto=3D"xZ5U_9OhBAAJ" rel=3D"nofollow" onmouse=
down=3D"this.href=3D&#39;javascript:&#39;;return true;" onclick=3D"this.hre=
f=3D&#39;javascript:&#39;;return true;">andrey....@gmail.<wbr>com</a>&gt;&g=
t; wrote:
<br>&gt;
<br>&gt; =C2=A0 =C2=A0 Fixing map/unordered_map operator[] instead is a les=
s radical and
<br>&gt; =C2=A0 =C2=A0 confusing approach. Let it return a proxy, which wil=
l either read or
<br>&gt; =C2=A0 =C2=A0 insert/assign the mapped value depending on the cont=
ext. Such a
<br>&gt; =C2=A0 =C2=A0 change is local to the library and does not require =
a core language
<br>&gt; =C2=A0 =C2=A0 modification.
<br>&gt;
<br>&gt; This is basically what the maps already do.
<br>
<br>I don&#39;t think so. According to the standard, opereator[] returns a=
=20
<br>reference to the mapped type, which implies the operator has to=20
<br>construct the element if it&#39;s not found. I was suggesting letting i=
t=20
<br>return an unspecified proxy object, which would have an overloaded=20
<br>operator=3D and operator T - the former would perform an insertion or=
=20
<br>assignment to the mapped object, the latter would perform a lookup or=
=20
<br>default-construction.
<br>
<br>In case of std::map, the default-construction behavior maintains=20
<br>backward compatibility with the current behavior. In user&#39;s code (i=
..e.=20
<br>in classes other than std::map &amp; friends) this behavior could be di=
fferent.
<br>
<br></blockquote><div><br>The problem with returning proxy objects is, as w=
ith many things, `auto`.<br><br>Let&#39;s say you have a `map&lt;string, T&=
gt;`. If you do something like `m[&quot;foo&quot;] =3D data;`, that works. =
It works because `std::string` is implicitly convertible from a string lite=
ral.<br><br>If you change `map::operator[]` to return a proxy (ignoring bac=
kwards compatibility issues here). The idea is that the proxy will effectiv=
ely call `insert_or_assign` with the value you give it. And it is implicitl=
y convertible to the value, default constructing it if it doesn&#39;t curre=
ntly exists.<br><br>But to do that, the proxy object needs two things: a re=
ference to the `map` that created it, and... the key the proxy is for. Is i=
t going to store that key as a reference? If so, then what happens when the=
 key is a temporary, like with `m[&quot;foo&quot;]`?<br><br>So if you do:<b=
r><br><div style=3D"background-color: rgb(250, 250, 250); border-color: rgb=
(187, 187, 187); border-style: solid; border-width: 1px; overflow-wrap: bre=
ak-word;" class=3D"prettyprint"><code class=3D"prettyprint"><div class=3D"s=
ubprettyprint"><span style=3D"color: #008;" class=3D"styled-by-prettify">au=
to</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> proxy <=
/span><span style=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify"> m</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">[</span><span style=3D"col=
or: #080;" class=3D"styled-by-prettify">&quot;foo&quot;</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">];</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify"><br>proxy </span><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">=3D</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"> T</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">{};</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"><br></span></div></code></div><br>Then `proxy` will have a=
 reference to a destroyed temporary. You could have `proxy` store the key b=
y value, but that makes `proxy` rather large.<br><br>It would also mean tha=
t you couldn&#39;t do `auto &amp;t =3D m[&quot;foo&quot;];`, since the retu=
rn value would undoubtedly be a prvalue. That makes the `operator[]` interf=
ace a lot less user-friendly.<br><br>Right now, C++ has too many problems w=
ith proxy objects to be able to use them in the way you&#39;re trying to us=
e them. What you really want is for `auto proxy =3D m[&quot;foo&quot;]` to =
deduce the `value_type`, not the proxy object&#39;s type. And right now, we=
 don&#39;t have a way to make that work.<br></div></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/8dab5ade-7159-4aa2-976a-c0086a904656%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/8dab5ade-7159-4aa2-976a-c0086a904656=
%40isocpp.org</a>.<br />

------=_Part_2277_260833761.1478009803062--

------=_Part_2276_874719072.1478009803061--

.


Author: Thiago Macieira <thiago@macieira.org>
Date: Tue, 01 Nov 2016 09:05:37 -0700
Raw View
On segunda-feira, 31 de outubro de 2016 21:48:34 PDT xaxxon@gmail.com wrote:
> Was wondering if there had been any conversation about adding an
> operator[]= which would be run in situations like
>
> my_type_object["asdf"] = 4;

Any proposal for:

 my_type_object["asdf"] += 4;
 my_type_object["asdf"] <<= 4;

And of course:

 my_type_object["asdf"] = my_type_object["asdf"];

--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel Open Source Technology Center

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/2365939.cLrk0U0liY%40tjmaciei-mobl1.

.


Author: Zhihao Yuan <zy@miator.net>
Date: Tue, 1 Nov 2016 15:54:09 -0500
Raw View
On Tue, Nov 1, 2016 at 11:05 AM, Thiago Macieira <thiago@macieira.org> wrote:
>> my_type_object["asdf"] = 4;
>
> Any proposal for:
>
>         my_type_object["asdf"] += 4;
>         my_type_object["asdf"] <<= 4;
>
> And of course:
>
>         my_type_object["asdf"] = my_type_object["asdf"];

The idea for operator[]= is not new, it presents in D, also
in Python.  As shown in this thread, it's just a syntax
sugar for a "insert" member function, but this syntax sugar
is very sweet, because it means "assignment operator
for a sub-object", which IMO is a missing piece in C++.

For the first question, if we are redesigning `std::map`,
its operator[] should behave like `std::vector`'s, thus
querying for a non-existing key invokes UB, then you
will find that m["key"] += obj just works perfectly, so
we don't need to have an operator[]+= when the class
have operator[]= defined, which clearly indicates that
this class wants to play with sub-object logic.

Same for m["key"] = m["key"], it's just
m.operator[]=("key", m.operator[]("key")).

For the existing `std::map`, I really don't have a good
advice other than breaking currently valid code :(
So maybe std2::map.

--
Zhihao Yuan, ID lichray
The best way to predict the future is to invent it.
___________________________________________________
4BSD -- http://blog.miator.net/

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAGsORuAgusFN0EEt%3DaGHNnP%3DAxrNuyUjm1RU75zkOBSRPbp04w%40mail.gmail.com.

.


Author: "D. B." <db0451@gmail.com>
Date: Tue, 1 Nov 2016 21:16:46 +0000
Raw View
--001a11469db6978fca054043d738
Content-Type: text/plain; charset=UTF-8

On Tue, Nov 1, 2016 at 8:54 PM, Zhihao Yuan <zy@miator.net> wrote:

>
> The idea for operator[]= is not new, it presents in D, also
> in Python.  As shown in this thread, it's just a syntax
> sugar for a "insert" member function, but this syntax sugar
> is very sweet, because it means "assignment operator
> for a sub-object", which IMO is a missing piece in C++.
>
>
Maybe I'm just missing the point over and over again (probably), but I feel
like the OP wants *construction* of the new index, not assignment. The
latter is already covered by various idioms.

Perhaps to help me understand it, could you show how this works in D and
Python? I know a small bit of the latter language and will hopefully find D
recognisable enough to see what's going on.

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CACGiwhE2j6DLfVhf9-gRgKGdH-dgRR7RgmNNKBWeuN6qw_JE2A%40mail.gmail.com.

--001a11469db6978fca054043d738
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"gmail_extra"><br><div class=3D"gmail_quote">=
On Tue, Nov 1, 2016 at 8:54 PM, Zhihao Yuan <span dir=3D"ltr">&lt;<a href=
=3D"mailto:zy@miator.net" target=3D"_blank">zy@miator.net</a>&gt;</span> wr=
ote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border=
-left:1px #ccc solid;padding-left:1ex"><span class=3D""><br>
</span>The idea for operator[]=3D is not new, it presents in D, also<br>
in Python.=C2=A0 As shown in this thread, it&#39;s just a syntax<br>
sugar for a &quot;insert&quot; member function, but this syntax sugar<br>
is very sweet, because it means &quot;assignment operator<br>
for a sub-object&quot;, which IMO is a missing piece in C++.<br>
<br></blockquote><div><br></div><div>Maybe I&#39;m just missing the point o=
ver and over again (probably), but I feel like the OP wants <i>construction=
</i> of the new index, not assignment. The latter is already covered by var=
ious idioms.<br><br></div><div>Perhaps to help me understand it, could you =
show how this works in D and Python? I know a small bit of the latter langu=
age and will hopefully find D recognisable enough to see what&#39;s going o=
n.<br>=C2=A0<br></div></div><br></div></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CACGiwhE2j6DLfVhf9-gRgKGdH-dgRR7RgmNN=
KBWeuN6qw_JE2A%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">htt=
ps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CACGiwhE2j6DLfVhf=
9-gRgKGdH-dgRR7RgmNNKBWeuN6qw_JE2A%40mail.gmail.com</a>.<br />

--001a11469db6978fca054043d738--

.


Author: Zhihao Yuan <zy@miator.net>
Date: Tue, 1 Nov 2016 16:40:57 -0500
Raw View
On Tue, Nov 1, 2016 at 4:16 PM, D. B. <db0451@gmail.com> wrote:
>>
>
> Maybe I'm just missing the point over and over again (probably), but I feel
> like the OP wants construction of the new index, not assignment. The latter
> is already covered by various idioms.

Ops, sorry, not "insert", but the new "insert_or_assign"
methods

  http://en.cppreference.com/w/cpp/container/map/insert_or_assign

, which will do construction for new indexes, and assignment
for existing ones.  I kept forgetting this long name; in my code
I simply call it "set".

>
> Perhaps to help me understand it, could you show how this works in D and
> Python? I know a small bit of the latter language and will hopefully find D
> recognisable enough to see what's going on.

In D it's opIndexAssign:

  https://dlang.org/spec/operatoroverloading.html#op-assign

and in Python it's setitem:

  https://docs.python.org/2/library/operator.html

D supports rewriting []@=, Python doesn't.

--
Zhihao Yuan, ID lichray
The best way to predict the future is to invent it.
___________________________________________________
4BSD -- http://blog.miator.net/

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAGsORuABD4w0uf6p%3DCE0HmiosS-Aj0%2Bk18-L1dxKRLqYZ3EyNw%40mail.gmail.com.

.


Author: Louis Dionne <ldionne.2@gmail.com>
Date: Tue, 1 Nov 2016 17:53:55 -0700 (PDT)
Raw View
------=_Part_4041_1857890915.1478048035732
Content-Type: multipart/alternative;
 boundary="----=_Part_4042_1232982673.1478048035733"

------=_Part_4042_1232982673.1478048035733
Content-Type: text/plain; charset=UTF-8

Nicol,

I think the specific problem you're referring to can be worked around by
only defining the assignment operator of the proxy for rvalue *this.
In general, however, I do agree with you that support for proxy types in
C++ is not great, and perhaps not sufficient to implement all the
corner cases that might come up.

#include <unordered_map>
#include <utility>
#include <string>

template <typename Map, typename Key>
struct proxy {
  Map& map;
  Key const& key;

  template <typename Value>
  Value& operator=(Value&& value) && {
    return map.insert_or_assign(key, std::forward<Value>(value)).first->
second;
  }
};

template <typename Key, typename Value>
struct unordered_map : std::unordered_map<Key, Value> {
  proxy<unordered_map, Key> operator[](Key const& key) {
    return {*this, key};
  }
};

struct no_default {
  no_default() = delete;
  std::string v;
};

int main() {
  unordered_map<std::string, no_default> map;
  map["foo"] = no_default{"bar"};
  auto proxy = map["baz"];
  proxy = no_default{"blip"}; // compiler error, no such operator=
}



On Tuesday, 1 November 2016 07:16:43 UTC-7, Nicol Bolas wrote:
>
> On Tuesday, November 1, 2016 at 5:58:14 AM UTC-4, Andrey Semashev wrote:
>>
>> On 11/01/16 12:46, D. B. wrote:
>> >
>> > On Tue, Nov 1, 2016 at 9:24 AM, Andrey Semashev
>> > <andrey....@gmail.com <mailto:andrey....@gmail.com>> wrote:
>> >
>> >     Fixing map/unordered_map operator[] instead is a less radical and
>> >     confusing approach. Let it return a proxy, which will either read
>> or
>> >     insert/assign the mapped value depending on the context. Such a
>> >     change is local to the library and does not require a core language
>> >     modification.
>> >
>> > This is basically what the maps already do.
>>
>> I don't think so. According to the standard, opereator[] returns a
>> reference to the mapped type, which implies the operator has to
>> construct the element if it's not found. I was suggesting letting it
>> return an unspecified proxy object, which would have an overloaded
>> operator= and operator T - the former would perform an insertion or
>> assignment to the mapped object, the latter would perform a lookup or
>> default-construction.
>>
>> In case of std::map, the default-construction behavior maintains
>> backward compatibility with the current behavior. In user's code (i.e.
>> in classes other than std::map & friends) this behavior could be
>> different.
>>
>>
> The problem with returning proxy objects is, as with many things, `auto`.
>
> Let's say you have a `map<string, T>`. If you do something like `m["foo"]
> = data;`, that works. It works because `std::string` is implicitly
> convertible from a string literal.
>
> If you change `map::operator[]` to return a proxy (ignoring backwards
> compatibility issues here). The idea is that the proxy will effectively
> call `insert_or_assign` with the value you give it. And it is implicitly
> convertible to the value, default constructing it if it doesn't currently
> exists.
>
> But to do that, the proxy object needs two things: a reference to the
> `map` that created it, and... the key the proxy is for. Is it going to
> store that key as a reference? If so, then what happens when the key is a
> temporary, like with `m["foo"]`?
>
> So if you do:
>
> auto proxy = m["foo"];
> proxy = T{};
>
> Then `proxy` will have a reference to a destroyed temporary. You could
> have `proxy` store the key by value, but that makes `proxy` rather large.
>
> It would also mean that you couldn't do `auto &t = m["foo"];`, since the
> return value would undoubtedly be a prvalue. That makes the `operator[]`
> interface a lot less user-friendly.
>
> Right now, C++ has too many problems with proxy objects to be able to use
> them in the way you're trying to use them. What you really want is for
> `auto proxy = m["foo"]` to deduce the `value_type`, not the proxy object's
> type. And right now, we don't have a way to make that work.
>

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/35158ecc-35ed-43f1-adf4-bb3db20c79e3%40isocpp.org.

------=_Part_4042_1232982673.1478048035733
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Nicol,<div><br></div><div>I think the specific problem you=
&#39;re referring to can be worked around by only defining the assignment o=
perator of the proxy for rvalue *this.</div><div>In general, however, I do =
agree with you that support for proxy types in C++ is not great, and perhap=
s not sufficient to implement all the</div><div>corner cases that might com=
e up.</div><div><br></div><div class=3D"prettyprint" style=3D"background-co=
lor: rgb(250, 250, 250); border: 1px solid rgb(187, 187, 187); word-wrap: b=
reak-word;"><code class=3D"prettyprint"><div class=3D"subprettyprint"><span=
 style=3D"color: #800;" class=3D"styled-by-prettify">#include</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"c=
olor: #080;" class=3D"styled-by-prettify">&lt;unordered_map&gt;</span><span=
 style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span style=
=3D"color: #800;" class=3D"styled-by-prettify">#include</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #080;" class=3D"styled-by-prettify">&lt;utility&gt;</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"color:=
 #800;" class=3D"styled-by-prettify">#include</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #080;" cla=
ss=3D"styled-by-prettify">&lt;string&gt;</span><span style=3D"color: #000;"=
 class=3D"styled-by-prettify"><br><br></span><span style=3D"color: #008;" c=
lass=3D"styled-by-prettify">template</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">&lt;</span><span style=3D"color: #008;" class=3D"styled-by=
-prettify">typename</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"> </span><span style=3D"color: #606;" class=3D"styled-by-prettify">=
Map</span><span style=3D"color: #660;" class=3D"styled-by-prettify">,</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span st=
yle=3D"color: #008;" class=3D"styled-by-prettify">typename</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"colo=
r: #606;" class=3D"styled-by-prettify">Key</span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">&gt;</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"><br></span><span style=3D"color: #008;" class=3D"=
styled-by-prettify">struct</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> proxy </span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">{</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"><br>=C2=A0 </span><span style=3D"color: #606;" class=3D"styled-by-prett=
ify">Map</span><span style=3D"color: #660;" class=3D"styled-by-prettify">&a=
mp;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> map</s=
pan><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span><span=
 style=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 </span><spa=
n style=3D"color: #606;" class=3D"styled-by-prettify">Key</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #008;" class=3D"styled-by-prettify">const</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">&amp;</span><span style=3D"color: #000;" c=
lass=3D"styled-by-prettify"> key</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">;</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"><br><br>=C2=A0 </span><span style=3D"color: #008;" class=3D"=
styled-by-prettify">template</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">&lt;</span><span style=3D"color: #008;" class=3D"styled-by-prettif=
y">typename</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
> </span><span style=3D"color: #606;" class=3D"styled-by-prettify">Value</s=
pan><span style=3D"color: #660;" class=3D"styled-by-prettify">&gt;</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 </span><=
span style=3D"color: #606;" class=3D"styled-by-prettify">Value</span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">&amp;</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #008;" class=3D"styled-by-prettify">operator</span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">=3D(</span><span style=3D"color: #606;"=
 class=3D"styled-by-prettify">Value</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">&amp;&amp;</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"> value</span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">)</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">&amp;&amp;</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">{</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 =C2=
=A0 </span><span style=3D"color: #008;" class=3D"styled-by-prettify">return=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> map</span=
><span style=3D"color: #660;" class=3D"styled-by-prettify">.</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify">insert_or_assign</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">key</span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;=
" class=3D"styled-by-prettify"> std</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify">forward</span><span style=3D"color: #660;" class=3D"styled=
-by-prettify">&lt;</span><span style=3D"color: #606;" class=3D"styled-by-pr=
ettify">Value</span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">&gt;(</span><span style=3D"color: #000;" class=3D"styled-by-prettify">va=
lue</span><span style=3D"color: #660;" class=3D"styled-by-prettify">)).</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify">first</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">-&gt;</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify">second</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">;</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"><br>=C2=A0 </span><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">}</span><span style=3D"color: #000;"=
 class=3D"styled-by-prettify"><br></span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">};</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"><br><br></span><span style=3D"color: #008;" class=3D"styled=
-by-prettify">template</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">&lt;</span><span style=3D"color: #008;" class=3D"styled-by-prettify">typ=
ename</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </sp=
an><span style=3D"color: #606;" class=3D"styled-by-prettify">Key</span><spa=
n 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;" class=3D"styled-by-prettify">typename</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #606;" cl=
ass=3D"styled-by-prettify">Value</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">&gt;</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"><br></span><span style=3D"color: #008;" class=3D"styled-b=
y-prettify">struct</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"> unordered_map </span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">:</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"> std</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify">unordered_=
map</span><span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</s=
pan><span style=3D"color: #606;" class=3D"styled-by-prettify">Key</span><sp=
an 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=
: #606;" class=3D"styled-by-prettify">Value</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">&gt;</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>=C2=A0 proxy</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">&lt;</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify">unordered_map</span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"> </span><span style=3D"color: #606;" class=3D"styled-by-prettify">Key</s=
pan><span style=3D"color: #660;" class=3D"styled-by-prettify">&gt;</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=
=3D"color: #008;" class=3D"styled-by-prettify">operator</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">[](</span><span style=3D"col=
or: #606;" class=3D"styled-by-prettify">Key</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=
=3D"styled-by-prettify">const</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">&amp;</span><span style=3D"color: #000;" class=3D"styled=
-by-prettify"> key</span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">)</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> =
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">{</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 =C2=A0 <=
/span><span style=3D"color: #008;" class=3D"styled-by-prettify">return</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">{*</span><span style=3D"=
color: #008;" class=3D"styled-by-prettify">this</span><span style=3D"color:=
 #660;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" c=
lass=3D"styled-by-prettify"> key</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">};</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"><br>=C2=A0 </span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">}</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"><br></span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">};</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br><b=
r></span><span style=3D"color: #008;" class=3D"styled-by-prettify">struct</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify"> no_default =
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">{</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 no_defau=
lt</span><span style=3D"color: #660;" class=3D"styled-by-prettify">()</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #0=
08;" class=3D"styled-by-prettify">delete</span><span style=3D"color: #660;"=
 class=3D"styled-by-prettify">;</span><span style=3D"color: #000;" class=3D=
"styled-by-prettify"><br>=C2=A0 std</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">::</span><span style=3D"color: #008;" class=3D"sty=
led-by-prettify">string</span><span style=3D"color: #000;" class=3D"styled-=
by-prettify"> v</span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">};</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify"><br><br></span><sp=
an style=3D"color: #008;" class=3D"styled-by-prettify">int</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"> main</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: #660;" cla=
ss=3D"styled-by-prettify">{</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"><br>=C2=A0 unordered_map</span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">&lt;</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify">std</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">::</span><span style=3D"color: #008;" class=3D"styled-by-=
prettify">string</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">,</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> no=
_default</span><span style=3D"color: #660;" class=3D"styled-by-prettify">&g=
t;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> map</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 map</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">[</span><span style=
=3D"color: #080;" class=3D"styled-by-prettify">&quot;foo&quot;</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: #6=
60;" class=3D"styled-by-prettify">=3D</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify"> no_default</span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">{</span><span style=3D"color: #080;" class=3D"s=
tyled-by-prettify">&quot;bar&quot;</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">};</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"><br>=C2=A0 </span><span style=3D"color: #008;" class=3D"sty=
led-by-prettify">auto</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"> proxy </span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">=3D</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
> map</span><span style=3D"color: #660;" class=3D"styled-by-prettify">[</sp=
an><span style=3D"color: #080;" class=3D"styled-by-prettify">&quot;baz&quot=
;</span><span style=3D"color: #660;" class=3D"styled-by-prettify">];</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 proxy =
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=3D</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify"> no_default</span=
><span style=3D"color: #660;" class=3D"styled-by-prettify">{</span><span st=
yle=3D"color: #080;" class=3D"styled-by-prettify">&quot;blip&quot;</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">};</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"colo=
r: #800;" class=3D"styled-by-prettify">// compiler error, no such operator=
=3D</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></s=
pan><span style=3D"color: #660;" class=3D"styled-by-prettify">}</span></div=
></code></div><div><div><br></div><div><br></div><br>On Tuesday, 1 November=
 2016 07:16:43 UTC-7, Nicol Bolas  wrote:<blockquote class=3D"gmail_quote" =
style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-l=
eft: 1ex;"><div dir=3D"ltr">On Tuesday, November 1, 2016 at 5:58:14 AM UTC-=
4, Andrey Semashev wrote:<blockquote class=3D"gmail_quote" style=3D"margin:=
0;margin-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex">On 11/01/1=
6 12:46, D. B. wrote:
<br>&gt;
<br>&gt; On Tue, Nov 1, 2016 at 9:24 AM, Andrey Semashev
<br>&gt; &lt;<a rel=3D"nofollow">andrey....@gmail.com</a> &lt;mailto:<a rel=
=3D"nofollow">andrey....@gmail.com</a>&gt;&gt; wrote:
<br>&gt;
<br>&gt; =C2=A0 =C2=A0 Fixing map/unordered_map operator[] instead is a les=
s radical and
<br>&gt; =C2=A0 =C2=A0 confusing approach. Let it return a proxy, which wil=
l either read or
<br>&gt; =C2=A0 =C2=A0 insert/assign the mapped value depending on the cont=
ext. Such a
<br>&gt; =C2=A0 =C2=A0 change is local to the library and does not require =
a core language
<br>&gt; =C2=A0 =C2=A0 modification.
<br>&gt;
<br>&gt; This is basically what the maps already do.
<br>
<br>I don&#39;t think so. According to the standard, opereator[] returns a=
=20
<br>reference to the mapped type, which implies the operator has to=20
<br>construct the element if it&#39;s not found. I was suggesting letting i=
t=20
<br>return an unspecified proxy object, which would have an overloaded=20
<br>operator=3D and operator T - the former would perform an insertion or=
=20
<br>assignment to the mapped object, the latter would perform a lookup or=
=20
<br>default-construction.
<br>
<br>In case of std::map, the default-construction behavior maintains=20
<br>backward compatibility with the current behavior. In user&#39;s code (i=
..e.=20
<br>in classes other than std::map &amp; friends) this behavior could be di=
fferent.
<br>
<br></blockquote><div><br>The problem with returning proxy objects is, as w=
ith many things, `auto`.<br><br>Let&#39;s say you have a `map&lt;string, T&=
gt;`. If you do something like `m[&quot;foo&quot;] =3D data;`, that works. =
It works because `std::string` is implicitly convertible from a string lite=
ral.<br><br>If you change `map::operator[]` to return a proxy (ignoring bac=
kwards compatibility issues here). The idea is that the proxy will effectiv=
ely call `insert_or_assign` with the value you give it. And it is implicitl=
y convertible to the value, default constructing it if it doesn&#39;t curre=
ntly exists.<br><br>But to do that, the proxy object needs two things: a re=
ference to the `map` that created it, and... the key the proxy is for. Is i=
t going to store that key as a reference? If so, then what happens when the=
 key is a temporary, like with `m[&quot;foo&quot;]`?<br><br>So if you do:<b=
r><br><div style=3D"background-color:rgb(250,250,250);border-color:rgb(187,=
187,187);border-style:solid;border-width:1px"><code><div><span style=3D"col=
or:#008">auto</span><span style=3D"color:#000"> proxy </span><span style=3D=
"color:#660">=3D</span><span style=3D"color:#000"> m</span><span style=3D"c=
olor:#660">[</span><span style=3D"color:#080">&quot;foo&quot;</span><span s=
tyle=3D"color:#660">];</span><span style=3D"color:#000"><br>proxy </span><s=
pan style=3D"color:#660">=3D</span><span style=3D"color:#000"> T</span><spa=
n style=3D"color:#660">{};</span><span style=3D"color:#000"><br></span></di=
v></code></div><br>Then `proxy` will have a reference to a destroyed tempor=
ary. You could have `proxy` store the key by value, but that makes `proxy` =
rather large.<br><br>It would also mean that you couldn&#39;t do `auto &amp=
;t =3D m[&quot;foo&quot;];`, since the return value would undoubtedly be a =
prvalue. That makes the `operator[]` interface a lot less user-friendly.<br=
><br>Right now, C++ has too many problems with proxy objects to be able to =
use them in the way you&#39;re trying to use them. What you really want is =
for `auto proxy =3D m[&quot;foo&quot;]` to deduce the `value_type`, not the=
 proxy object&#39;s type. And right now, we don&#39;t have a way to make th=
at work.<br></div></div></blockquote></div></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/35158ecc-35ed-43f1-adf4-bb3db20c79e3%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/35158ecc-35ed-43f1-adf4-bb3db20c79e3=
%40isocpp.org</a>.<br />

------=_Part_4042_1232982673.1478048035733--

------=_Part_4041_1857890915.1478048035732--

.