Search code examples
c++boostsyntaxc-preprocessorboost-preprocessor

Is Boost using legal C++ preprocessing directive syntax?


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?


Solution

  • 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.