#pragma

Sue Meloy sue at hprsue.HP.COM
Fri Nov 30 11:07:51 AEST 1990


Norman Diamond writes:
> Not at all.  A program which wants to be conforming and executable on all
> implementations, but obtain some feature from an implementation which
> recognizes a particular #pragma, should use #pragma.  A program which
> DEPENDS on a non-portable construction should use an extended #directive
> and/or __identifier that is offered by the particular implementation.
>  
> This is exactly why the standard says that if an implementation does not
> recognize a particular #pragma, it should ignore it.  If an implementation
> does not recognize a #funny_directive or __reserved_identifier, it is
> supposed to issue a diagnostic.  Implementation-dependent programs that
> are coded in Mr. Unni's style will not receive the desired diagnostics
> from other implementations.
>  
> The instructions that an unrecognized #pragma be ignored are now either
> useless or, worse, counterproductive.  Unfortunately, the ANSI committee
> seems to have forgotten their own wishes.

There are still pitfalls even with #pragma.  Just because an implementation
"recognizes" the pragma does not mean that it will do what the programmer
intends.  One implementation may interpret #pragma list to mean that
a program listing should be generated, while another could interpret this to
mean that this month's accounts payable transactions should be dumped to 
the line printer.

The only safe thing is for all implementation-specific directives 
to be protected by ifdefs.  Thus, a conforming program can
contain pragmas which could affect the behavior of a strictly conforming 
program, but can continue to be compiled on an implementation that does not
make use of those pragmas.

For example (no noalias diatribes, please!),

do_something(int *vector1, int *vector2)
#ifdef WHIZBANG_C
#pragma noalias vector1,vector2
#endif
{
   /* do some stuff that could be vectorized if vector1 and vector2
    * are not aliased to one another.
    */
}

If WHIZBANG_C is not defined, this code must be accepted and run as 
specified by the standard on all conforming implementations.
Whether it does the right thing on WHIZBANG_C is a quality-of-implementation/
quality-of-programmer issue.

This brings up another question:
If a non-standard directive name is used instead, is a conforming
implementation required to accept it if it appears inside a skipped
section of code?  

The standard does not appear to be completely clear
on this point; it says that in this case "... directives are processed
only through the name that determines the directive in order to keep
track of the level of nested conditionals...".  My gut feel is that
even this limited amount of processing would require a diagnostic if 
the directive name is not standard.

Sue Meloy
sue at hpcllca.cup.hp.com



More information about the Comp.std.c mailing list