Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is the three-way comparison operator (C++20) supported in Qt 6.9 (clang and libc++) for `QString`, `QDate` and `QTime`?
Forum Updated to NodeBB v4.3 + New Features

Is the three-way comparison operator (C++20) supported in Qt 6.9 (clang and libc++) for `QString`, `QDate` and `QTime`?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 77 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pehg
    wrote last edited by
    #1

    I'm implementing a three-way comparison operator for a class. In such implementation, I use the <=> operator with some QString, QDate and QTime objects. According to this post https://www.qt.io/blog/c20-comparisons-in-qt, the operator was added to those classes in Qt 6.7. It's not in the documentation, though.

    I have 3 setups described in the table below. The program compiles and runs perfectly in setups 2 and 3. However, it fails in setup 1.

    Setups

    Setup 1 Setup 2 Setup 3
    OS freebsd 14 opensuse tumbleweed tuxedo OS (ubuntu-based)
    Compiler Clang 18 GCC 15 GCC 15
    C++ standard C++23 C++23 C++23
    Std lib libc++ libstdc++ libstdc++
    Qt 6.9 6.9 6.8
    Type CI job CI job local machine
    Result Compilation failure Success Success

    Error

    error: invalid operands to binary expression ('QDate' and 'QDate')
      142 |     if (const auto cmp = other.lastMessageDateTime.date() <=> lastMessageDateTime.date(); cmp != std::strong_ordering::equal) {
          |                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    error: invalid operands to binary expression ('QTime' and 'QTime')
      145 |     if (const auto cmp = other.lastMessageDateTime.time() <=> lastMessageDateTime.time(); cmp != std::strong_ordering::equal) {
          |                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    error: invalid operands to binary expression ('QString' and 'QString')
      149 |     if (const auto cmp = displayName() <=> other.displayName(); cmp != std::strong_ordering::equal) {
          |                          ~~~~~~~~~~~~~ ^   ~~~~~~~~~~~~~~~~~~~
    
    error: invalid operands to binary expression ('const QString' and 'const QString')
      153 |     return accountJid <=> other.accountJid;
          |            ~~~~~~~~~~ ^   ~~~~~~~~~~~~~~~~
    

    I tried compiling the project using clang on my local machine and it worked, but I had to use libstdc++. Apparently, I would need to compile Qt with libc++ to be able to use libc++. So, it's not the same configuration as the CI job.

    So far the only solution I've found is to write an implementation that avoids using <=> with such Qt classes. I don't have a clue what could be going on in this case.

    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote last edited by
      #2

      Does your Makefile explicitely use the clang option for c++20? Might it be defaulting to an older standard? Look at your generated Makefile for CXXOPTS or some such variant.

      I light my way forward with the fires of all the bridges I've burned behind me.

      P 1 Reply Last reply
      0
      • Kent-DorfmanK Kent-Dorfman

        Does your Makefile explicitely use the clang option for c++20? Might it be defaulting to an older standard? Look at your generated Makefile for CXXOPTS or some such variant.

        P Offline
        P Offline
        pehg
        wrote last edited by
        #3

        @Kent-Dorfman I don't think so, because in the same implementation I use the <=> operator with a member variable that is of type int. Additionally, there is no problem with the usage of std::strong_ordering and the project uses other C++20 features. The compiler complaints only about those Qt types using <=>.

        SGaistS 1 Reply Last reply
        0
        • P pehg

          @Kent-Dorfman I don't think so, because in the same implementation I use the <=> operator with a member variable that is of type int. Additionally, there is no problem with the usage of std::strong_ordering and the project uses other C++20 features. The compiler complaints only about those Qt types using <=>.

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote last edited by
          #4

          @pehg hi,

          While taking a quick look at the header file mentioned in the article I stumbled upon this issue on the llvm project that might explain the situation.

          I think you don't have __cpp_lib_three_way_comparison defined with clang 18.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • P Offline
            P Offline
            pehg
            wrote last edited by
            #5

            @SGaist Hi,
            I remember I saw that LLVM issue but I didn't know how to interpret it and I thought it was fixed. However, I just put the static asserts below in the code and compilation failed in the CI using clang and libc++, while it succeeded in the rest using GCC and libstdc++.

            #include <compare>
            
            // Check if the types support three-way comparison
            static_assert(std::three_way_comparable<QString>, " ** QString - three-way comparison NOT supported");
            static_assert(std::three_way_comparable<QDate>, " ** QDate - three-way comparison NOT supported");
            static_assert(std::three_way_comparable<QTime>, " ** QTime - three-way comparison NOT supported");
            
            // Check if the C++20 three-way comparison is available
            static_assert(__cpp_lib_three_way_comparison >= 201907L, " ** Three-way comparison library NOT available");
            

            I don't know if you refer to qcompare.h and qcomparehelpers.h. I just looked at them and I found usage of __cpp_lib_three_way_comparison . So, most likely my problem is due to the libc++ issue in the link you mentioned.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved