473,890 Members | 1,705 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
Apr 17 '24 #1
0 11215

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

Similar topics

2
11600
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 a dedicated function in the string object, or (+1) an operator? Apologies if I have missed...
2
1512
by: Mike - EMAIL IGNORED | last post by:
I wrote: class Parent { ... private: virtual Parent& operator=(const Parent& parent); }; class Child : public Parent
13
4657
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 MyString's correctly in the map. But a subsequent call to map.find("hello") returns map.end(). Even more...
3
7381
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
5710
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
1381
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 numerics as well. Example: a = range(5)
5
1975
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 expression
2
3509
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
1382
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
2051
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 spec it will return all the records smaller than 120 in the database "AND laptops.hard_drive...
0
11215
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10802
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10451
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9618
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8008
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7160
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5835
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4665
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
2
4259
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.