473,519 Members | 2,592 Online
Bytes | Software Development & Data Engineering Community
+ 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
4 Weeks Ago #1
0 7432

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

Similar topics

2
11550
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 as quite verbose for such a common operation, and possibly inneficient since it would create and destroy 2 tempory objects. Perhaps there should be...
2
1461
by: Mike - EMAIL IGNORED | last post by:
I wrote: class Parent { ... private: virtual Parent& operator=(const Parent& parent); }; class Child : public Parent
13
4583
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 which also implements ==, <, <=, >, >=, etc. (Those operators are tested and working correctly.) If I assign map = "world", it saves the...
3
7323
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 were
5
5664
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> class String {
4
1339
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 few other things, such as type checking, and I was just wondering whether or not it is considered bad practice in the Python Community to use it for...
5
1927
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 isProfit >From VehicleFinance but I get an error 'Incorrect syntax near >' Books online states that the select_list can contain column_name or...
2
3459
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 equal. Google returns nothing, you are my only hope xD Thanks, Austen :-)
6
1332
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= (Intersection &i) { this->ID = i.ID;
12
1977
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 harddrive criteria. Is there something wrong with this bit of code or should i be taking a different approach. The idea is that if the hardrive is low...
0
7297
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7200
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7432
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, 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. ...
1
7159
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5739
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3281
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
1643
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
834
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
505
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.