13

My (relatively old) C++ compiler choked on this file in Boost, which starts out as:

# /* Copyright (C) 2001
#  * Housemarque Oy
#  * http://www.housemarque.com
#  *
#  * Distributed under the Boost Software License, Version 1.0. (See
#  * accompanying file LICENSE_1_0.txt or copy at
#  * http://www.boost.org/LICENSE_1_0.txt)
#  */
#

Is this really legal C++? What's the rule on the syntax of preprocessor tokens?

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
user541686
  • 189,354
  • 112
  • 476
  • 821

1 Answers1

22

Yes, a line containing only # and whitespace is explicitly permitted by the standard §16 [cpp]:

control-line:
   # include pp-tokens new-line
   # define identifier replacement-list new-line
   # define identifier lparen identifier-listopt) replacement-list new-line
   # define identifier lparen ... ) replacement-list new-line
   # define identifier lparen identifier-list, ... ) replacement-list new-line
   # undef identifier new-line
   # line pp-tokens new-line
   # error pp-tokensopt new-line
   # pragma pp-tokensopt new-line
   # new-line

Note that comments are replaced by whitespace at translation phase 3, that is before the preprocessor.

Yakov Galka
  • 61,035
  • 13
  • 128
  • 192
  • 1
    +1. The last one is all that I was looking for, and actually answers the question : `# new-line`. – Nawaz Aug 11 '12 at 07:35
  • 3
    @Nawaz: the OP asks "What's the rule on the syntax of preprocessor tokens" so providing some context won't hurt. – Yakov Galka Aug 11 '12 at 07:38
  • @Nawaz: Haha take a look at the edit history on my post, it was amusing. :) – user541686 Aug 11 '12 at 07:38
  • @Mehrdad: Haha.. we were doing the same thing at the same time, hence SO merged them. – Nawaz Aug 11 '12 at 07:39
  • 4
    Why is boost commenting and adding blank preprocessor token? Is there any advantage in doing that instead of using just simple regular block comment? – Lie Ryan Aug 11 '12 at 07:43
  • 5
    @LieRyan: look at the file linked in question. The author decided that it is easier to start *all* lines in the file with `#`. It's just such a coding style. Personally I don't like it. – Yakov Galka Aug 11 '12 at 08:06