Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #72816
| From | James Kuyper <jameskuyper@alumni.caltech.edu> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Help needed |
| Date | 2020-04-04 22:27 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <r6bfmp$ti3$1@dont-email.me> (permalink) |
| References | (4 earlier) <slrnr8fcd2.eb4.nedlatham@woden.valhalla.oz> <r691l0$idd$1@dont-email.me> <slrnr8g4vd.ep1.nedlatham@woden.valhalla.oz> <r69sbm$l63$1@dont-email.me> <slrnr8h34h.fe4.nedlatham@woden.valhalla.oz> |
On 4/4/20 9:33 AM, Ned Latham wrote:
> Alf P. Steinbach wrote:
>> On 04.04.2020 06:58, Ned Latham wrote:
>>> James Kuyper wrote:
>>>> Ned Latham wrote:
>>>>> Jorgen Grahn wrote:
>>>>>> Ned Latham wrote:
...
>> e.g. about lifetime of temporaries.
>
> I always regard those as lasting until their return.
I have no idea what "their return" is supposed to mean. The general rule
is that temporaries last until the end of the full-expression that
contains them. There's three main exceptions, the most important of
which occurs when a temporary is bound to a reference.
>>>> Could you identify what you consider to be defective about C++'s
>>>> post-increment semantic?
>>>
>>> It doesn't work with non-native types.
>>> Post-decrement too, of course.
>>
>> You just have to defined custom operators where you want custom
>> operators and then, if properly defined, they work fine.
>
> Nope. With non-native types passed to functions they pass the
> incremented value to the function.
Well, he did say "if properly defined". To reproduce behavior comparable
to that for the built-in types, you need merely declare the operator
overload as returning a value of the same type, and define the operator
overload to create a copy of the current object before incrementing it,
and to return that copy. You won't get the behavior you've described
above if you follow those rules.
>> struct Non_native
>> {
>> double amount;
>>
>> operator double() const { return amount; }
>
> What's this for? It's not used, below.
That implements a conversion from Non-native to double, and it is used
below.
>>
>> void advance()
>> {
>> amount += 0.01;
>> }
>>
>> Non_native operator++( int )
>
> double is non-native?
No, but Non_native contains a double, and is implicitly convertible to a
double. I'm curious - how did you misinterpret the above code so as to
lead to that question?
>> {
>> Non_native result = *this;
>> advance();
>> return result;
>> }
>> };
>>
>> int main()
>> {
>> Non_native o = {3.14};
>>
>> cout << "Originally " << o++ << "." << endl;
You said that operator double() was not used. It might help you
understand this code to realize that it's equivalent to the following:
cout << "Originally " << o.operator++(1).operator double()
<< "." << endl;
If you don't understand why those two statements are equivalent, please
explain what part you do understand, and we can fill in the rest.
>> cout << "After: " << o << "." << endl;
Similarly, that line is equivalent to:
cout << "After: " << o.operator double() << "." << endl;
>> }
>
> I'm not even going to try it, Alf, And I bet you haven't done so.
> That code requires overload operators = and << for the struct.
No, the rules of C++ cause operator double() to be called in each of the
relevant cases, and there are operators already defined for << working
on doubles. There's no need to declare or define operator=(); C++ rules
cause it to be implicitly declared and implicitly defined, and the
implicitly defined operator=() does precise what is needed. That's not
a coincidence - those rule were created specifically to save developers
the trouble of defining operator=() explicitly. You only need to
explicitly declare or define it in circumstance the implicit
declaration/definition doesn't do what you want it to do.
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Help needed Gerry Wolff <gerrywolff65@gmail.com> - 2020-04-03 09:56 -0700
Re: Help needed Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2020-04-03 18:18 +0100
Re: Help needed Ned Latham <nedlatham@woden.valhalla.oz> - 2020-04-03 12:57 -0500
Re: Help needed Melzzzzz <Melzzzzz@zzzzz.com> - 2020-04-03 18:17 +0000
Re: Help needed Ned Latham <nedlatham@woden.valhalla.oz> - 2020-04-03 16:41 -0500
Re: Help needed James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-04-03 14:58 -0400
Re: Help needed Ned Latham <nedlatham@woden.valhalla.oz> - 2020-04-03 16:45 -0500
Re: Help needed Ned Latham <nedlatham@woden.valhalla.oz> - 2020-04-03 17:52 -0500
Re: Help needed Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-04-03 20:05 +0000
Re: Help needed Ned Latham <nedlatham@woden.valhalla.oz> - 2020-04-03 16:58 -0500
Re: Help needed Ned Latham <nedlatham@woden.valhalla.oz> - 2020-04-03 17:50 -0500
Re: Help needed James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-04-04 00:15 -0400
Re: Help needed Ned Latham <nedlatham@woden.valhalla.oz> - 2020-04-03 23:58 -0500
Re: Help needed Ian Collins <ian-news@hotmail.com> - 2020-04-04 18:06 +1300
Re: Help needed Ned Latham <nedlatham@woden.valhalla.oz> - 2020-04-04 00:35 -0500
Re: Help needed Ian Collins <ian-news@hotmail.com> - 2020-04-04 19:54 +1300
Re: Help needed Ned Latham <nedlatham@woden.valhalla.oz> - 2020-04-04 02:22 -0500
Re: Help needed Bo Persson <bo@bo-persson.se> - 2020-04-04 10:33 +0200
Re: Help needed Ned Latham <nedlatham@woden.valhalla.oz> - 2020-04-04 04:55 -0500
Re: Help needed David Brown <david.brown@hesbynett.no> - 2020-04-04 13:02 +0200
Re: Help needed Ned Latham <nedlatham@woden.valhalla.oz> - 2020-04-04 07:15 -0500
Re: Help needed David Brown <david.brown@hesbynett.no> - 2020-04-04 16:49 +0200
Re: Help needed Ned Latham <nedlatham@woden.valhalla.oz> - 2020-04-05 04:10 -0500
Re: Help needed Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2020-04-05 16:31 +0100
Re: Help needed Ned Latham <nedlatham@woden.valhalla.oz> - 2020-04-05 11:37 -0500
Re: Help needed Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-04 14:19 +0200
Re: Help needed David Brown <david.brown@hesbynett.no> - 2020-04-04 16:57 +0200
Re: Help needed James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-04-04 10:51 -0400
Re: Help needed James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-04-04 10:44 -0400
Re: Help needed Ned Latham <nedlatham@woden.valhalla.oz> - 2020-04-05 04:06 -0500
Re: Help needed James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-04-05 11:04 -0400
Re: Help needed Ned Latham <nedlatham@woden.valhalla.oz> - 2020-04-05 11:31 -0500
Re: Help needed James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-04-05 16:59 -0400
Re: Help needed Ned Latham <nedlatham@woden.valhalla.oz> - 2020-04-05 21:38 -0500
Re: Help needed "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2020-04-04 13:51 +0200
Re: Help needed Ned Latham <nedlatham@woden.valhalla.oz> - 2020-04-04 08:33 -0500
Re: Help needed Ian Collins <ian-news@hotmail.com> - 2020-04-05 07:35 +1200
Re: Help needed Ned Latham <nedlatham@woden.valhalla.oz> - 2020-04-05 04:19 -0500
Re: Help needed James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-04-05 11:27 -0400
Re: Help needed James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-04-04 22:27 -0400
Re: Help needed Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2020-04-04 14:50 +0100
Re: Help needed James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-04-04 10:27 -0400
Re: Help needed Ian Collins <ian-news@hotmail.com> - 2020-04-05 07:37 +1200
Re: Help needed Ned Latham <nedlatham@woden.valhalla.oz> - 2020-04-05 03:45 -0500
Re: Help needed Paavo Helde <myfirstname@osa.pri.ee> - 2020-04-04 09:02 +0300
Re: Help needed Ned Latham <nedlatham@woden.valhalla.oz> - 2020-04-04 01:20 -0500
Re: Help needed Ian Collins <ian-news@hotmail.com> - 2020-04-04 19:43 +1300
Re: Help needed Ned Latham <nedlatham@woden.valhalla.oz> - 2020-04-04 02:10 -0500
Re: Help needed Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-04-04 01:58 -0700
Re: Help needed Ned Latham <nedlatham@woden.valhalla.oz> - 2020-04-04 05:04 -0500
Re: Help needed Paavo Helde <myfirstname@osa.pri.ee> - 2020-04-04 14:37 +0300
Re: Help needed Ned Latham <nedlatham@woden.valhalla.oz> - 2020-04-04 07:25 -0500
Re: Help needed Paavo Helde <eesnimi@osa.pri.ee> - 2020-04-04 17:39 +0300
Re: Help needed Ned Latham <nedlatham@woden.valhalla.oz> - 2020-04-05 04:02 -0500
Re: Help needed Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-03 19:35 +0200
Re: Help needed Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-03 20:01 +0200
Re: Help needed Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-04 16:16 +0200
Re: Help needed Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-04 18:58 +0200
Re: Help needed Christian Gollwitzer <auriocus@gmx.de> - 2020-04-03 22:01 +0200
Re: Help needed Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2020-04-03 21:05 +0100
Re: Help needed Bo Persson <bo@bo-persson.se> - 2020-04-04 02:10 +0200
Re: Help needed Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-04-04 08:19 +0000
Re: Help needed Gerry Wolff <gerrywolff65@gmail.com> - 2020-04-05 02:56 -0700
Re: Help needed Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-04-04 01:14 +0100
Re: Help needed Gerry Wolff <gerrywolff65@gmail.com> - 2020-04-05 02:49 -0700
Re: Help needed Sam <sam@email-scan.com> - 2020-04-04 16:30 -0400
Re: Help needed Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2020-04-05 05:58 +0100
Re: Help needed Gerry Wolff <gerrywolff65@gmail.com> - 2020-04-05 03:21 -0700
Re: Help needed Juha Nieminen <nospam@thanks.invalid> - 2020-04-07 06:33 +0000
Re: Help needed Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-04-07 10:39 +0000
csiph-web