Perukii
Perukii

Reputation: 41

Can I use three ways comparison (<=>) using g++?

I want to try to use the three ways comparison operator (<=>) but...

code:

#include <compare>

int main(){
  .
  .
  .
}

command:

g++ -std=c++2a compare.cpp

output:

compare.cpp:10:10: fatal error: compare: No such file or directory
   10 | #include <compare>
      |          ^~~~~~~~~
compilation terminated.

I couldn't find any solution to fix this error. I’m using g++ 9.3.0.

Hasn't g++ supported <compare> yet? If it has, how can I fix this error using g++? Or if it hasn't, please teach me another way to use <compare>, without g++.

Upvotes: 1

Views: 1204

Answers (1)

einpoklum
einpoklum

Reputation: 131519

Can I use three ways comparison (<=>) using g++?

Yes, this does work.

But remember that, as commenters note:

  1. It has to be a sufficiently recent version of GCC - 10.x at least
  2. This header, and the spaceship operator <=>, are part of the C++20 language standard. So you have to specify -std=c++20 (or -std=c++2a in earlier versions) to ensure they can be used.

Upvotes: 4

Related Questions