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

  • A+
Category:Languages

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?

 


*= 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).

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: