Why preprocessor commands should start as first non-white space

I am trying to execute the path of the #ifndef part through the setter string and I got this error

"Error 20 error C2014: preprocessor command must start as first non-white space"

I am aware of the errors, I am just wondering why this is so? Is this a compiler choice? What are the reasons for this? What is easier for the user to notice?

Here is the code in case anyone is interested:

inline void SetSomething(int value) { #ifndef XBOX ASSERT(value <= 1); #endif test = value; };

      

+3


source to share


3 answers


At first, C did not have a standard preprocessor. Then people started using pre-processing as an external tool. You may notice that #

matches up with comments generally in Unix-land shell scripts.

As the language developed, the preprocessor integrated with the compiler and most of the language itself, but it retained its completely different structure, namely, in particular, that it is line oriented, and the main languages ​​C and C ++ are free form.



After that, the lines are a little blurry. Now preprocessing usually adds directives #line

or equivalent for use by the host language compiler, also directives #pragma

are for host language compiler, and in the other direction we have _Pragma

(IIRC). However, the structure is basically the same as it was originally. C and C ++ are developed languages, not programmed languages.

+5


source


Taking a look at the standard (section 16 "Pre-processing Directives") starting with the # character as the frirst non whitespace character is what makes the preprocessing directive by definition.



A preprocessing directive consists of a sequence of preprocessing tokens that satisfies the following constraints: The first token in the sequence is a preprocessing token, which (at the beginning of translation phase 4) is either the first character in the source file (optionally after a space, containing no newlines ) or a space followed by at least one newline character.

+2


source


If you want the most important reason, it's because the standard says so.

If you want to know why the standard says so, this is the easiest way to get the functionality you need.
Remember that preprocessing and compilation are two potentially completely separate tasks, and the preprocessor has no idea about the language of its output.

+1


source







All Articles