2

Is the following code allowed in c++? a. foo() (note the space before "foo")

I would assume not, but the compiler doesn't complain.

Rivka
  • 823
  • 2
  • 7
  • 20

4 Answers4

3

Should be fine, as far as I know the compiler strips out all whitespace (tab, newline, space) unless they are in a String (i.e. "in a string").

[edit] you should also not put whitespace in operators (i.e. i++ is different from i+ + and foo() is different from fo o()).

[edit] As mentioned in another answer, whitespace (spaces, tabs, newlines, comments) is also used to separate operators such as void bar() vs. voidbar()

Von Lion
  • 748
  • 4
  • 22
  • 4
    Nope. `x + = 42;` wouldn't be compiled. It strips whitespaces only between **tokens** – borisbn Mar 14 '13 at 13:24
  • They are stripped by lexer, because he generates tokens – kassak Mar 14 '13 at 13:27
  • 1
    The compiler doesn't strip _any_ white space. The front end breaks the input up into tokens, ignoring white space _between_ tokens (but no where else). You can introduce white space before and after the `.` in the example because the `.` is a token. You cannot introduce extra white space in `foo` or `++`, because `foo` and `++` are tokens. You must insert white space in `voidbar` because otherwise, the maximum munch rule would make it a single token. (All of this on the results of the preprocessor, of course.) – James Kanze Mar 14 '13 at 14:00
3

Yes, it is valid C++ code:

From C++ Standard - ANSI ISO IEC 14882 2003.pdf, chapter 2.6:

There are five kinds of tokens: identifiers, keywords, literals, operators, and other separators. Blanks, horizontal and vertical tabs, newlines, formfeeds, and comments (collectively, “white space”), as described below, are ignored except as they serve to separate tokens.

The same chapter defines that a punctuator is also a token.

Chapter 2.12 Operators and punctuators defines that . is a punctuator.

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
2

Allowed.

. is just operator, like +, ::, ->, && and others.

Spaces are ignored in your case

Generally speaking spaces removed after lexer generates tokens from source file.

kassak
  • 3,974
  • 1
  • 25
  • 36
  • That's weird. How about "<::" vs "< ::" ? It does make a difference in my experience ... "<:" may be interpreted as "[". Spaces are not always ignored, or is there something I didn't catch ? –  Mar 14 '13 at 13:25
  • Don't forget the maximum munch rule. When breaking the source up into tokens, the scanner will read as far forward as possible, independently of any surrounding context. So given `"<::"`, it first tries for the longest token, which is `"<:"` (a synonym for `"'"`). Which means that `"<::"`, is `"["`, `":"`, and not `"<"`, `"::"`. Despite the fact that `"<"`, `"::"` might result in a legal progream, but `"["`, `":"` never can. – James Kanze Mar 14 '13 at 14:05
  • @JamesKanze really so, but I only stated that after lexer pass there is no more spaces =) – kassak Mar 14 '13 at 14:08
2

You can have as many or as few spaces anywhere before or after any token in C or C++. The only rule is that tokens need to be separated by either a different token (that is, something not a letter (A-Z, a-z, _) or a digit [and where relevant, a token may also contain ., + or -, such as floating point numbers).

So

 a.foo()
 a. foo();
 a . foo (    ) ;
 a
   . 
foo 
  (
 )
;

are all the same.

But you have to have a space between certain tokens, particularly those consisting of only "letters & digits" (see above):

voidfoo()

is not the same as

void foo();
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227