3

I am attempting to make use of c++20's new Three-Way operator(<=>) capabilities in my programs, but it appears that this operator is not defined for strings, as this program

#include <compare>
#include <string>

using namespace std;

int main()
{

    string a = "a";
    string b = "b";
    auto x = (a <=> b);

    return 0;
}

produces this error:

Error   C2676   binary '<=>': 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator

However, according to cppreference, this operation should be defined in c++20: https://en.cppreference.com/w/cpp/string/basic_string/operator_cmp

Three-Way comparison operator works fine with primitive types, as changing the first two variables to int compiles without issue, additionally default-implemented Three-Way comparison operator is working correctly with classes, but only when those classes contain primitive types only. The same problem is happening with std::vector as well, which should support Three-Way comparison operator from what I understand.

I am using cmake, and I have correctly set the standard to C++ 20 as far as I am aware

cmake_minimum_required (VERSION 3.8)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)

I am compiling using MSVC in visualstudio

  • 2
    More important than Cmake, what compiler are you using? I don't think any major compiler has _fully_ implemented C++20 yet, and it wouldn't surprise me if GCC or something just doesn't have `<=>` for `std::string` yet. – Nathan Pierson May 22 '21 at 04:13
  • 2
    For MSVC specifically, [according to MS](https://learn.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance?view=msvc-160) "adding spaceship `<=>` to the library" is listed as "partial in VS 2019 16.7" with a footnote that doesn't seem to have more detail. Sounds like it's just the compiler not having a complete C++20 implementation yet. – Nathan Pierson May 22 '21 at 04:15
  • VS 2016 16.10 preview 3 claims to be std library c++20 complete. Not for production code though. – doug May 22 '21 at 04:25
  • VS 2019 16.10 has been released for general availability. "Our compiler and STL are now feature-complete for the latest available C++20 standard! 16.10 comes with a few much-anticipated features: calendars, timezones and ." – doug May 26 '21 at 20:21

0 Answers0