/* #include <someyhing.h> */ => include it or do not include it?That is the question ....

  • Thread starter Andreas Bogenberger
  • Start date
A

Andreas Bogenberger

Hi all,

I stumbled on the following example:

/*
#include <something.h>
#include <even_more.h>
*/

gpp expands the #include statements. I'm not really sure how to interpret
this correctly. As I got this like written above, I suppose it is working
on an other platform. However, I have found nothing in 6.4.9, 6.4.7
(however R3 mentions an undefined behavior if /* is between the braces or
double quotes of the include directive) or 6.10 and I am getting to the
conclusion that this is an undefined behavior. Is my assumption right?

best regards,
Andreas
 
B

Ben Bacarisse

Andreas Bogenberger said:
Hi all,

I stumbled on the following example:

/*
#include <something.h>
#include <even_more.h>
*/

gpp expands the #include statements.

[What is gpp -- a typo for gcc?]
I'm not really sure how to interpret
this correctly.

The standard lays out a sequence of translation phases that help make
this clear. The implementation must behave as if it have these
phases.

The two that relate to the above are 3 and 4:

3. The source file is decomposed into preprocessing tokens6) and
sequences of white-space characters (including comments). A
source file shall not end in a partial preprocessing token or in
a partial comment. Each comment is replaced by one space
character. New-line characters are retained. Whether each
nonempty sequence of white-space characters other than new-line
is retained or replaced by one space character is
implementation-defined.

4. Preprocessing directives are executed, macro invocations are
expanded, and _Pragma unary operator expressions are executed. If
a character sequence that matches the syntax of a universal
character name is produced by token concatenation (6.10.3.3), the
behavior is undefined. A #include preprocessing directive causes
the named header or source file to be processed from phase 1
through phase 4, recursively. All preprocessing directives are
then deleted.

It seems clear from this that phase 4 won't see any include directives
in your example -- just a space where the comment was.
As I got this like written above, I suppose it is working
on an other platform. However, I have found nothing in 6.4.9, 6.4.7
(however R3 mentions an undefined behavior if /* is between the braces or
double quotes of the include directive) or 6.10 and I am getting to the
conclusion that this is an undefined behavior. Is my assumption
right?

It is well-defined.
 
K

Keith Thompson

Andreas Bogenberger said:
I stumbled on the following example:

/*
#include <something.h>
#include <even_more.h>
*/

gpp expands the #include statements. I'm not really sure how to interpret
this correctly. As I got this like written above, I suppose it is working
on an other platform. However, I have found nothing in 6.4.9, 6.4.7
(however R3 mentions an undefined behavior if /* is between the braces or
double quotes of the include directive) or 6.10 and I am getting to the
conclusion that this is an undefined behavior. Is my assumption right?

If the code is exactly as you posted it, then the include directives
are part of a comment and should be ignored.

My best guess is that the code is not exactly as you posted it, that
there's some subtle difference between the actual code and what you
posted (perhaps something you assumed was insignificant).
 
A

Andreas Bogenberger

If the code is exactly as you posted it, then the include directives are
part of a comment and should be ignored.

My best guess is that the code is not exactly as you posted it, that
there's some subtle difference between the actual code and what you
posted (perhaps something you assumed was insignificant).

gpp -> typo for cpp

your guess was of course right. I did not see a comment on one of the
include lines had a comment because line wrapping was off and it was far
to the right side of the screen.

thank you for your answers,
Andreas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top