Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a bug in clang++/g++? [duplicate]

Tags:

c++

Given:

void function(int*=0) {}
int main() {
}

clang (3.8.0):

test.cc:1:18: error: expected ')'
void function(int*=0) {
                 ^

g++ (5.4.0):

test.cc:1:18: error: expected ‘,’ or ‘...’ before ‘*=’ token
 void function(int*=0) {
                  ^    

If I change it to (note the spacing):

void function(int* = 0) {}
int main() {
}

Obviously it's getting confused whether I'm typing T* = 0 or T *= 0, is this a bug or expected?

like image 775
gct Avatar asked May 31 '18 16:05

gct


People also ask

Does clang define __ GNUC __?

(GNU C is a language, GCC is a compiler for that language.Clang defines __GNUC__ / __GNUC_MINOR__ / __GNUC_PATCHLEVEL__ according to the version of gcc that it claims full compatibility with.

What is clang CL?

● The driver provides convenience and compatibility ● clang-cl is a cl.exe compatible driver mode for clang ● It understands the environment, the flags, and the tools ● Integrates with Visual Studio ● /fallback allows bring-up of large projects.

Why does Clang use GCC?

Clang is designed to provide a frontend compiler that can replace GCC. Apple Inc. (including NeXT later) has been using GCC as the official compiler. GCC has always performed well as a standard compiler in the open source community.

Are GCC and Clang interchangeable?

TL;DR: Clang is highly compatible to GCC - just give it a go. In most cases, Clang could be used as a GCC drop in replacement ( clang and clang++ are "GCC compatible drivers").


1 Answers

*= is one operator, like += is. So x *= 2; is the same as x = x * 2;

You want * = to be lexed as two tokens (conceptually in C++, lexing happens before, and nearly independently, of parsing; read about C++ translation phases).

like image 85
Basile Starynkevitch Avatar answered Oct 16 '22 18:10

Basile Starynkevitch