Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #91543
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: What a bug |
| Date | 2023-09-05 11:12 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <ud6rep$1tq9a$2@dont-email.me> (permalink) |
| References | <uct7ch$3uope$1@dont-email.me> <ud08l2$i4aq$1@dont-email.me> <ud2t4q$12vk6$1@dont-email.me> <87pm2y6af4.fsf@nosuchdomain.example.com> <ud6qom$1tniq$1@dont-email.me> |
Am 05.09.2023 um 11:01 schrieb jak:
> Keith Thompson ha scritto:
>> jak <nospam@please.ty> writes:
>>> jak ha scritto:
>>>> Bonita Montero ha scritto:
>>>>> I checked for some commandline options and I thought it would be the
>>>>> best to map that through an unordered_map. I extracted this from my
>>>>> application in the below code.
>>>>>
>>>>> #include <Windows.h>
>>>>> #include <iostream>
>>>>> #include <variant>
>>>>> #include <unordered_map>
>>>>>
>>>>> using namespace std;
>>>>>
>>>>> int wmain( int argc, wchar_t **argv )
>>>>> {
>>>>> static unordered_map<wchar_t const *, DWORD>
>>>>> const opts =
>>>>> {
>>>>> { L"--idle", IDLE_PRIORITY_CLASS },
>>>>> { L"--below", BELOW_NORMAL_PRIORITY_CLASS },
>>>>> { L"--above", ABOVE_NORMAL_PRIORITY_CLASS },
>>>>> { L"--high", HIGH_PRIORITY_CLASS },
>>>>> { L"--realtime", REALTIME_PRIORITY_CLASS }
>>>>> };
>>>>> auto mappedPrio = opts.find( "--high" );
>>>>> if( mappedPrio == opts.end() )
>>>>> return -1;
>>>>> return 0;
>>>>> }
>>>>>
>>>>> Why does my code have a bug and why doesn't mappedPrio
>>>>> point to end() at the end?
>>>> HI,
>>>> you just need to use wchar_t in your search string as well:
>>>> from:
>>>> auto mappedPrio = opts.find( "--high" );
>>>> to:
>>>> auto mappedPrio = opts.find( L"--high" );
>>>>
>>>
>>> mmm. This only works because the compiler uses the same area of memory
>>> as the strings are equal and therefore compares a pointer to itself when
>>> it finds the string. In fact if I replace this:
>>>
>>> auto mappedPrio = opts.find( L"--high" );
>>>
>>> with this other:
>>>
>>> wchar_t what[] = L"--high";
>>> auto mappedPrio = opts.find( what );
>>>
>>> the search fails.
>>>
>>> I find this sneaky. In my opinion the compiler shouldn't treat two
>>> objects as the same just because they have the same content.
>>
>> The find() function of unordered_map<> compares keys for *equality*.
>>
>> Since the code defines the key type as `wchar_t const *`, it does
>> pointer comparison, which has all the usual problems when you apply it
>> to string literals; L"foo" and L"foo" may or may not be stored at the
>> same memory location, and L"foo" and a string L"foo" derived from user
>> input will certainly not have the same address.
>>
>> You can specify an equality function if you like.
>>
>> A far simpler solution is to use std::wstring as the key type.
>>
>
> HI,
> I probably expressed myself badly. I agree with what you answered and
> everything is clear to me. What I am contesting is the part where you
> say "...may or may not be stored at the same memory location" because
> that would mean that a program like this:
>
> #include <iostream>
>
> int main()
> {
> auto eif = [] (bool t) {
> std::cout << ( t ? "equal" : "not equal" ) << std::endl;
> };
>
> char const *sp = "hello";
> char sl[] = "hello";
Try to make s1 static const, then it may be joined.
>
> eif( sp == sp );
>
> eif( "hello" == sp );
>
> eif( "hello" == "hello" );
>
> eif( "hello" == sl );
>
> return 0;
> }
>
> which has this as a result:
>
> equal
> equal
> equal
> not equal
>
> could give different results depending on how the compiler acts when
> deciding where to store strings (because here we are comparing
> pointers). I simply think that different objects shouldn't be lumped
> together just because they contain the same things, otherwise I would
> have declared a single string and would have used its pointer around the
> code. Pointers are for that too. it is my opinion that the program I
> posted should have this as a result:
>
> equal
> not equal
> not equal
> not equal
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
What a bug Bonita Montero <Bonita.Montero@gmail.com> - 2023-09-01 19:35 +0200
Re: What a bug "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2023-09-01 23:09 +0200
Re: What a bug "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-09-01 14:52 -0700
Re: What a bug Bonita Montero <Bonita.Montero@gmail.com> - 2023-09-02 06:23 +0200
Re: What a bug Muttley@dastardlyhq.com - 2023-09-02 10:25 +0000
Re: What a bug Bonita Montero <Bonita.Montero@gmail.com> - 2023-09-02 12:56 +0200
Re: What a bug Bonita Montero <Bonita.Montero@gmail.com> - 2023-09-02 14:35 +0200
Re: What a bug Bonita Montero <Bonita.Montero@gmail.com> - 2023-09-02 14:41 +0200
Re: What a bug Muttley@dastardlyhq.com - 2023-09-02 15:30 +0000
Re: What a bug Bonita Montero <Bonita.Montero@gmail.com> - 2023-09-02 18:10 +0200
Re: What a bug "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-09-01 14:50 -0700
Re: What a bug Bonita Montero <Bonita.Montero@gmail.com> - 2023-09-02 06:19 +0200
Re: What a bug "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-09-03 15:25 -0700
Re: What a bug Bonita Montero <Bonita.Montero@gmail.com> - 2023-09-04 04:53 +0200
Re: What a bug "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-09-03 20:01 -0700
Re: What a bug Bonita Montero <Bonita.Montero@gmail.com> - 2023-09-04 06:59 +0200
Re: What a bug Bonita Montero <Bonita.Montero@gmail.com> - 2023-09-04 11:11 +0200
Re: What a bug "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-09-04 18:48 -0700
Re: What a bug "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-09-14 16:27 -0700
Re: What a bug scott@slp53.sl.home (Scott Lurndal) - 2023-09-04 14:24 +0000
Re: What a bug "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-09-18 16:20 -0700
Re: What a bug Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2023-09-04 18:43 -0400
Re: What a bug red floyd <no.spam.here@its.invalid> - 2023-09-04 17:36 -0700
Re: What a bug Bonita Montero <Bonita.Montero@gmail.com> - 2023-09-05 05:49 +0200
Re: What a bug Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2023-09-06 12:00 -0400
Re: What a bug Bonita Montero <Bonita.Montero@gmail.com> - 2023-09-06 19:46 +0200
Re: What a bug Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2023-09-06 14:44 -0400
Re: What a bug Bonita Montero <Bonita.Montero@gmail.com> - 2023-09-07 04:51 +0200
Re: What a bug Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2023-09-06 23:54 -0400
Re: What a bug Bonita Montero <Bonita.Montero@gmail.com> - 2023-09-07 06:58 +0200
Re: What a bug Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2023-09-07 11:08 -0400
Re: What a bug Bonita Montero <Bonita.Montero@gmail.com> - 2023-09-07 17:56 +0200
Re: What a bug Bonita Montero <Bonita.Montero@gmail.com> - 2023-09-07 18:04 +0200
Re: What a bug Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2023-09-07 19:35 -0400
Re: What a bug Bonita Montero <Bonita.Montero@gmail.com> - 2023-09-07 17:46 +0200
Re: What a bug jak <nospam@please.ty> - 2023-09-02 23:15 +0200
Re: What a bug jak <nospam@please.ty> - 2023-09-03 23:17 +0200
Re: What a bug Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-09-03 17:37 -0700
Re: What a bug jak <nospam@please.ty> - 2023-09-05 11:01 +0200
Re: What a bug Bonita Montero <Bonita.Montero@gmail.com> - 2023-09-05 11:12 +0200
Re: What a bug Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-09-05 03:04 -0700
Re: What a bug jak <nospam@please.ty> - 2023-09-07 01:18 +0200
Re: What a bug James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-09-08 00:09 -0400
Re: What a bug Bonita Montero <Bonita.Montero@gmail.com> - 2023-09-05 11:11 +0200
Re: What a bug red floyd <no.spam.here@its.invalid> - 2023-09-05 20:53 -0700
Re: What a bug Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-09-05 21:14 -0700
Re: What a bug red floyd <no.spam.here@its.invalid> - 2023-09-06 17:51 -0700
Re: What a bug Bonita Montero <Bonita.Montero@gmail.com> - 2023-09-06 06:49 +0200
csiph-web