473,485 Members | 1,399 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem With Comparison Operator <=> in G++

Oralloy
988 Recognized Expert Contributor
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
Expand|Select|Wrap|Line Numbers
  1. g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in bit_field.cpp:
Expand|Select|Wrap|Line Numbers
  1. //
  2. //  bit_field.cpp
  3. //  - demonstrate problem with bit field comparisons
  4. //
  5.  
  6. #include <cstdint>
  7. #include <iostream>
  8.  
  9. struct thing_t
  10. {
  11.   ::std::uint32_t value : 4;
  12.   auto operator<=>(thing_t const &that) const
  13.   {
  14.     return  (this->value <=> that.value);
  15.   }
  16. };
  17.  
  18. int main()
  19. {
  20.   thing_t s0{1};
  21.   thing_t s1{2};
  22.  
  23.   ::std::cout
  24.     << ::std::boolalpha
  25.     << "s0 -- s1 ? " << ((s0 <=> s1) == 0) << "\n";
  26.  
  27.   return 0;
  28. }
  29.  
And, here are the errors which I see:
Expand|Select|Wrap|Line Numbers
  1. bit_field.cpp: In member function ‘auto thing_t::operator<=>(const thing_t&) const’:
  2. bit_field.cpp:14:20: warning: narrowing conversion of ‘((const thing_t*)this)->thing_t::value’ from ‘const uint32_t’ {aka ‘const unsigned int’} to ‘int’ [-Wnarrowing]
  3.    14 |     return  (this->value <=> that.value);
  4.       |              ~~~~~~^~~~~
  5. bit_field.cpp:14:35: warning: narrowing conversion of ‘that.thing_t::value’ from ‘const uint32_t’ {aka ‘const unsigned int’} to ‘int’ [-Wnarrowing]
  6.    14 |     return  (this->value <=> that.value);
  7.       |                              ~~~~~^~~~~
  8.  
Needless to say, I am confused.
My understanding from what I have read is that "value" would be promoted to a fully unsigned int32_t value, which ought be comparable to another of the same type.
My gut reaction is that I have encountered a compiler bug, however I have found only a few in my career, so likely not.
Obviously I have done something stupid, as I can usually read multiple sources of documentation and ascertain where I encroached into undefined behaviour.
Can any of you provide explanatory links or enlighten me?
Thank You,
Oralloy
3 Weeks Ago #1
0 7111

Sign in to post your reply or Sign up for a free account.

Similar topics

2
11545
by: Will McGugan | last post by:
What is the best way to do a simple case insensitive comparison in Python? As far as I can tell, the only option is to do str1.lowercase() == str2.lowercase() (or uppercase). But that strikes me...
2
1452
by: Mike - EMAIL IGNORED | last post by:
I wrote: class Parent { ... private: virtual Parent& operator=(const Parent& parent); }; class Child : public Parent
13
4573
by: jstanforth | last post by:
This is probably a very obvious question, but I'm not clear on what operators need to be implemented for std::map.find() to work. For example, I have a class MyString that wraps std::string, and...
3
7314
by: nectar | last post by:
Hello In VBA I need to count records from an Offers table, that were created beetwen 2 dates (strDate1 and strDate2). Using the DCount as follow gives me strange results: records that...
5
5654
by: Karl | last post by:
Hey everyone! So I'm writing my own String class to wrap std::string and implement an API as close to identical as possible to the Java API. It's pretty small so far: #include <string> ...
4
1332
by: Mike Duffy | last post by:
I just recently realized that the comparison operator "is" actually works for comparing numeric values. Now, I know that its intended use is for testing object identity, but I have used it for a...
5
1922
by: Jim Devenish | last post by:
I want to create a column alias to represent the comparison of two columns (ie a boolean result of True or False). A simple example is: Select VehicleFinanceID, SalePrice > PurchasePrice As...
2
3447
by: sake | last post by:
Simply put, I need to know how to overload a comparison operator like ==. Assuming I have a function that compares to of my objects: obj1, and obj2. This function returns 0 if obj1 and obj2 are...
6
1323
by: Lilith | last post by:
I have a class called Intersection which contains the following with public access... Intersection operator= (Intersection &i); It's defined as... Intersection Intersection::operator=...
12
1970
by: panteraboy | last post by:
Hi there again folks. Ps thanks for all the help gettin me this far. I get an 3075 syntax error (missing operator) in the following code of the click event. The code worked fine before i added the...
0
7111
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7155
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6820
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7258
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5410
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4856
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3053
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
593
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
242
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.