Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #79414

Re: Overloading the [] operator for read/write...

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c++
Subject Re: Overloading the [] operator for read/write...
Date 2021-05-04 18:34 +0200
Organization A noiseless patient Spider
Message-ID <s6rt2m$9an$1@dont-email.me> (permalink)
References <s67oai$cm6$1@dont-email.me> <c214cbb7-330e-4a88-865d-8c35c1c9931dn@googlegroups.com> <s6p7fr$63t$1@dont-email.me>

Show all headers | View raw


On 03/05/2021 18:13, Paavo Helde wrote:
> 03.05.2021 17:57 daniel...@gmail.com kirjutas:
> 
>> As C++ is currently specified, proxy abstractions produce surprising
>> behavior when combined with auto, e.g.
>>
>> auto x = mydb[y];
>>
>> would return a value of type MyDBProxy_, not X. [...]
>> For your use case, the notational convenience
>> of operator[] doesn't compensate for the additional complexity and
>> incompatibility with auto.
> 
> As a relatively new feature, I'm afraid auto tends to be overused
> currently in some places. Seeing the actual type of the variable in code
> is a huge benefit for the reader. Using 'auto' is only justified if the
> actual type is both verbose and either obvious or not important. In the
> OP-s example:
> 
> X x = mydb[y];
> 
> (this would work fine when there is a proper conversion operator defined
> from proxy to X).
> 
> Here, X is a single class name, so it ought to be not too verbose even
> when (hopefully) replaced with a bit longer and more descriptive name in
> real code. This type is also most probably important and it might be not
> obvious for a casual reader who is not familiar with what exactly mydb
> is and does.
> 
> So there is no real reason to use auto here, IMO. I can understand why
> the proposal for getting auto working here would not get much traction
> in the committee, it looks more like a great feature for obfuscated C++
> contests.

I would go further and say that you definitely /want/ auto to give the
proxy object, not the conversion, in a case like this.  When
initialising from "mydb[y]", sometimes you would want the converted
value, and sometimes the proxy object - and when you want the converted
value you can, as you say, write "X x = mydb[y];".  If you want the
proxy itself then it would be a good use of "auto" - the type is likely
to be verbose and its details would be a clutter in the code.

Back to comp.lang.c++ | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Overloading the [] operator for read/write... TDH1978 <thedeerhunter1978@movie.uni> - 2021-04-26 21:10 -0400
  Re: Overloading the [] operator for read/write... Paavo Helde <myfirstname@osa.pri.ee> - 2021-04-27 14:02 +0300
    Re: Overloading the [] operator for read/write... TDH1978 <thedeerhunter1978@movie.uni> - 2021-04-30 17:08 -0400
      Re: Overloading the [] operator for read/write... Paavo Helde <myfirstname@osa.pri.ee> - 2021-05-02 22:15 +0300
    Re: Overloading the [] operator for read/write... Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-03 09:27 +0200
  Re: Overloading the [] operator for read/write... "daniel...@gmail.com" <danielaparker@gmail.com> - 2021-05-03 07:57 -0700
    Re: Overloading the [] operator for read/write... Paavo Helde <myfirstname@osa.pri.ee> - 2021-05-03 19:13 +0300
      Re: Overloading the [] operator for read/write... "daniel...@gmail.com" <danielaparker@gmail.com> - 2021-05-03 11:33 -0700
      Re: Overloading the [] operator for read/write... David Brown <david.brown@hesbynett.no> - 2021-05-04 18:34 +0200

csiph-web