Is the following code allowed in c++?
a. foo()
(note the space before "foo")
I would assume not, but the compiler doesn't complain.
Is the following code allowed in c++?
a. foo()
(note the space before "foo")
I would assume not, but the compiler doesn't complain.
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()
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.
Allowed.
.
is just operator, like +
, ::
, ->
, &&
and others.
Spaces are ignored in your case
Generally speaking spaces removed after lexer generates tokens from source file.
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();