1

I have this case:

int a=5;
int b=6;
int *p_a=&a;
int *p_b=&b

std::cout << *p_a / *p_b;

which seems fine. However, if I delete the space between the division symbol / and *p_a like this:

std::cout << *p_a /*p_b;

The compiler understands /* as comment.

Is this behavior a standard one? I do not even have */ as a closing symbol of the comment.

Note: I am on MSVS 2013.

Edit:

It seems to be a standard behaviour since SO code formating has considered it as a comment also :D

Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160
  • 1
    The C++ tokenizer is supposed to be *greedy* and collect as many characters it can into tokens. So the compiler is correct. It doesn't matter that there's no closing `*/`, that will just make the compiler complain about an unterminated comment or something similar. – Some programmer dude Jan 23 '17 at 06:48
  • no it is not compiling I am getting compile error and intellisense errors : error C1071: unexpected end of file found in comment IntelliSense: comment unclosed at end of file – Humam Helfawi Jan 23 '17 at 06:48
  • @Someprogrammerdude you mean as many characters in one token or as many tokens in the whole translation unit? – Humam Helfawi Jan 23 '17 at 06:49
  • 1
    As many characters as possible to form a single token. In this case it sees `/`,`*` and `p` without separation, and since there is no token `/*p` it backtracks to `/*`. – Some programmer dude Jan 23 '17 at 06:51

1 Answers1

1

It is standard behaviour. Draft n4296 says in 2.7 Comments [lex.comment]

The characters /* start a comment, which terminates with the characters */. These comments do not nest...

That means that if you remove the space between / and * any conformant C++ compiler will see it as the leading sequence of a (here unterminated) comment.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252