Click here to Skip to main content
15,883,901 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Invalid comparator for STL map Pin
Daniel Pfeffer25-Dec-22 19:36
professionalDaniel Pfeffer25-Dec-22 19:36 
GeneralRe: Invalid comparator for STL map Pin
ForNow25-Dec-22 21:33
ForNow25-Dec-22 21:33 
GeneralRe: Invalid comparator for STL map Pin
ForNow26-Dec-22 5:14
ForNow26-Dec-22 5:14 
GeneralRe: Invalid comparator for STL map Pin
Graham Breach26-Dec-22 6:15
Graham Breach26-Dec-22 6:15 
JokeRe: Invalid comparator for STL map Pin
ForNow26-Dec-22 6:51
ForNow26-Dec-22 6:51 
GeneralRe: Invalid comparator for STL map Pin
ForNow26-Dec-22 7:39
ForNow26-Dec-22 7:39 
GeneralRe: Invalid comparator for STL map solution Pin
ForNow26-Dec-22 8:31
ForNow26-Dec-22 8:31 
GeneralRe: Invalid comparator for STL map solution Pin
k505426-Dec-22 15:49
mvek505426-Dec-22 15:49 
That has a couple of issues. Firstly, you are invoking undefined behavior: Union declaration - cppreference.com
Secondly, you're expecting that the bit patterns of 2 doubles can be compared. This may not be the case, as you may a NaN this way, and then any comparison will return false:
Shell
[k5054@localhost tmp]$ cat example.c 
#include <stdio.h>

int main()
{
    double d = 0.0 / 0.0;  // produces NaN 
    printf("1.0 < d = %d\n", 1.0 < d);
    printf("d < 1.0 = %d\n", d < 1.0);
    printf("d == d  = %d\n", d == d);
}
[k5054@localhost tmp]$ gcc example.c -o example
[k5054@localhost tmp]$ ./example 
1.0 < d = 0
d < 1.0 = 0
d == d  = 0
[k5054@localhost tmp]$
As we can see in the above code, NaN compared to anything, even itself returns false.

If all you want to do is compare bits, you might as well just go ahead and use memcmp()
C++
*#35;include <cstring>
struct usingrange {
    ESDID esdid;
    int start;
    int end;
    // ... other stuff ...
    bool operator<(const usingrange& x)const {
        return ::memcmp(this, &x, sizeof(*this)
    }
};
This relies on the fact that the sizeof a struct or a class is the size of its individual members. This only becomes tricky if your struct/class includes STL or other objects like std::string or std::vector.
Keep Calm and Carry On

GeneralRe: Invalid comparator for STL map solution Pin
ForNow26-Dec-22 21:06
ForNow26-Dec-22 21:06 
GeneralRe: Invalid comparator for STL map solution Pin
Graham Breach26-Dec-22 21:42
Graham Breach26-Dec-22 21:42 
GeneralRe: Invalid comparator for STL map solution Pin
ForNow27-Dec-22 1:22
ForNow27-Dec-22 1:22 
QuestionRead Access exception in xtree for map::insert Pin
ForNow24-Dec-22 17:18
ForNow24-Dec-22 17:18 
AnswerRe: Read Access exception in xtree for map::insert answer found Pin
ForNow25-Dec-22 9:55
ForNow25-Dec-22 9:55 
Questionoverloaded = operator not being invoked Pin
ForNow22-Dec-22 14:36
ForNow22-Dec-22 14:36 
AnswerRe: overloaded = operator not being invoked Pin
Mircea Neacsu22-Dec-22 15:24
Mircea Neacsu22-Dec-22 15:24 
GeneralRe: overloaded = operator not being invoked Pin
ForNow22-Dec-22 15:28
ForNow22-Dec-22 15:28 
GeneralRe: overloaded = operator not being invoked Pin
Mircea Neacsu22-Dec-22 15:37
Mircea Neacsu22-Dec-22 15:37 
GeneralRe: overloaded = operator not being invoked Pin
ForNow22-Dec-22 16:00
ForNow22-Dec-22 16:00 
GeneralRe: overloaded = operator not being invoked Pin
Mircea Neacsu22-Dec-22 16:32
Mircea Neacsu22-Dec-22 16:32 
GeneralRe: overloaded = operator not being invoked Pin
ForNow22-Dec-22 16:41
ForNow22-Dec-22 16:41 
QuestionStill Struggling with map::insert and operator overload Pin
ForNow20-Dec-22 15:57
ForNow20-Dec-22 15:57 
AnswerRe: Still Struggling with map::insert and operator overload Pin
Mircea Neacsu20-Dec-22 17:34
Mircea Neacsu20-Dec-22 17:34 
GeneralRe: Still Struggling with map::insert and operator overload Pin
ForNow21-Dec-22 1:51
ForNow21-Dec-22 1:51 
QuestionMessage Closed Pin
18-Dec-22 5:01
ForNow18-Dec-22 5:01 
AnswerRe: Question about return value of STD MAP::INSERT Pin
Mircea Neacsu18-Dec-22 6:55
Mircea Neacsu18-Dec-22 6:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.