473,714 Members | 2,513 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 9306

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

Similar topics

2
11577
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
1485
by: Mike - EMAIL IGNORED | last post by:
I wrote: class Parent { ... private: virtual Parent& operator=(const Parent& parent); }; class Child : public Parent
13
4628
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
7358
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
5691
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
1361
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
1955
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
3486
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
1364
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
2014
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
8795
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8701
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9068
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9009
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
7942
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...
0
5943
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
4462
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...
0
4715
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2510
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.