Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #68013
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Weird use of maps. |
| Date | 2019-08-30 08:28 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <qkafmk$vs8$1@dont-email.me> (permalink) |
| References | <qk5vm0$52n$1@dont-email.me> <87ftllb15b.fsf@ID-208667.user.individual.de> <qk791e$a2h$4@dont-email.me> <871rx33kkd.fsf@ID-208667.user.individual.de> <qk9pog$3hu$1@dont-email.me> |
On 30/08/2019 02:14, James Kuyper wrote: > On 8/29/19 2:14 PM, Stefan Große Pawig wrote: >> [some context restored] >> >> James Kuyper <jameskuyper@alumni.caltech.edu> writes: >>> On 8/28/19 9:08 PM, James Kuyper wrote: >>>> On 8/28/19 2:22 PM, Stefan Große Pawig wrote: >>>>> Is that map of yours stored in another QVariant? >>>>> >>>> No. The original message map was declared as QMap<QString, QVariant>, >>>> and the QVariant values were filled in with part maps of the type >>>> QMap<QString, int>. I successfully changed the message map to >>> Correction: QMap<QString, QVariant>. >> >> But then, IIUC, that fits the bill: the QMap<QString, QVariant> for the >> parts /was/ stored in another QVariant (namely, as the value type of the >> message map). >> >> If the code used QVariant::value() to fetch the part map from the message >> map, there is no restriction on the type of the part map. The restriction >> only arises if the caller used QVariant::toMap(). >> >> Regardless of what the code originally used, maybe the original author >> had just scanned the QVariant interface for anything related to maps, >> found only the QMap<QString, QVariant> as baked-in return value and then >> assumed that this was the only map type that could be stored in a >> QVariant? > The key point is that the author of the code freely chose to declare the > part map as QMap<QString, QVariant>, where the QVariant was used > exclusively to hold integers. He also freely choose to declare the > message map as QMap<QString, QVariant>, where the QVariant was used to > store a part map. There was no code outside the code he wrote which gave > him any excuse for using QVariant for both purposes - no code outside of > what he wrote used QVariant to interface with the code he did wrote. > Yes, he did use toMap(), to extract the map from the QVariant, but he > was the one who wrote the code to put the map into the QVariant. > > If he'd used QMap<QString, int> for the part map, and QMap<QSTring, > QMap<QString, int> > for the message map, it would all have worked fine > (except for the bug that I fixed), and he would have been able to > extract the map directly, rather than by calling toMap(). So why did he > use QVariant instead? I'm getting the impression that he used QVariant > without bothering to consider whether it was actually needed. > I can imagine a few possible explanations. One is that, as I mentioned earlier, either the code or the programmer comes from a background of dynamically typed language - he may simply have felt comfortable with variant types. Perhaps he was less used to complex types and found "QMap<QString, QVariant>" to be simpler (in his eyes) than "QMap<QSTring, QMap<QString, int> >". For a long time, templates had a reputation for program size bloat in C++. (In the failed "EC++" "Embedded C++" standard, templates were banned for that reason. Though $DEITY only knows why namespaces were banned too.) Some people were left with the impression that you should avoid instantiating too many different template types, to reduce code bloat and duplication. Maybe he thought re-using a single map type was more efficient in program size than using two different types. (I'm not suggesting that this is a /good/ reason, even if it were the case that it would give smaller code - something I doubt.)
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Weird use of maps. James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-08-28 09:30 -0400
Re: Weird use of maps. Jens Kallup <jkallup@web.de> - 2019-08-28 16:22 +0200
Re: Weird use of maps. James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-08-28 20:46 -0400
Re: Weird use of maps. Öö Tiib <ootiib@hot.ee> - 2019-08-28 09:00 -0700
Re: Weird use of maps. "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2019-08-28 14:54 -0700
Re: Weird use of maps. James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-08-28 20:58 -0400
Re: Weird use of maps. David Brown <david.brown@hesbynett.no> - 2019-08-29 10:24 +0200
Re: Weird use of maps. James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-08-29 10:05 -0400
Re: Weird use of maps. Mike Terry <news.dead.person.stones@darjeeling.plus.com> - 2019-08-28 19:19 +0100
Re: Weird use of maps. James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-08-28 21:05 -0400
Re: Weird use of maps. usenet@stegropa.de (Stefan Große Pawig) - 2019-08-28 20:22 +0200
Re: Weird use of maps. James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-08-28 21:08 -0400
Re: Weird use of maps. James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-08-28 21:16 -0400
Re: Weird use of maps. usenet@stegropa.de (Stefan Große Pawig) - 2019-08-29 20:14 +0200
Re: Weird use of maps. James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-08-29 20:14 -0400
Re: Weird use of maps. David Brown <david.brown@hesbynett.no> - 2019-08-30 08:28 +0200
Re: Weird use of maps. Öö Tiib <ootiib@hot.ee> - 2019-08-30 00:15 -0700
Re: Weird use of maps. Manfred <noname@add.invalid> - 2019-09-13 17:49 +0200
Re: Weird use of maps. James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-09-13 15:04 -0400
Re: Weird use of maps. Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-08-28 19:33 +0000
Re: Weird use of maps. James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-08-28 21:11 -0400
csiph-web