Saturday, February 26, 2022

[SOLVED] C++20 feature not working Ubuntu 20.04 LTS (three-way comparison)

Issue

I am trying to run the following code snippet on Sublime-Text on Ubuntu 20.4 LTS.

#include <iostream>
int main(){
    auto result = (10 <=> 5) > 0;
    std::cout << result << std::endl;
    return 0;
}

But this results in

ModernC++.cpp: In function ‘int main()’:
ModernC++.cpp:3:22: error: expected primary-expression before ‘>’ token
    3 |  auto result = (10 <=> 5) > 0;
      |                      ^
[Finished in 271ms with exit code 1]
[cmd: ['g++ -std=c++2a ModernC++.cpp -o ModernC++ && timeout 7s ./ModernC++<input.txt>output.txt']]
[dir: /home/parth/.config/sublime-text/Packages/User]
[path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin]

I have also tried running it directly from the terminal with the following command

g++ ModernC++.cpp -std=c++2a -o ModernC++

But this results in

ModernC++.cpp: In function ‘int main()’:
ModernC++.cpp:3:22: error: expected primary-expression before ‘>’ token
    3 |  auto result = (10 <=> 5) > 0;

I have tried the same code on my Windows 10 OS and it works fine there. I did a bit of research. I have found out that C++20 features are available from GCC 8 onwards. I checked my G++ version as well.

g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

So I do not think version is the issue. Here is build-system file for sublime as well if someone can tell me a mistake in that as well.

{
    "cmd" : ["g++ -std=c++2a $file_name -o $file_base_name && timeout 7s ./$file_base_name<input.txt>output.txt"],
    "selector" : "source.cpp",
    "shell":true,
    "working_dir" : "$file_path"
} 

So here are my queries -

  1. How do I fix the code not running in Sublime-Text?
  2. How do I fix the code not running in Terminal?

I searched for fixes but only issue I found was version being too low and clearly that is not the case with my system. Also, C++17 standard code is working fine.

EDIT 1: changed g++ to g++-10, still the same error.

EDIT 2: Fixed, <=> was mistakenly changed to <= > for some reason.


Solution

You need atleast version 10 in GCC. check the compiler support https://en.cppreference.com/w/cpp/compiler_support



Answered By - balu
Answer Checked By - Mary Flores (WPSolving Volunteer)