In C++ (and C) we have the #pragma
directive which basically has implementation defined effects. However, are there any limits of what the directive may do? (Note that I'm asking about what the standard allows, not about what real compilers actually do.)
What I'm certain #pragma
may do:
What I would guess is allowed, but am not sure:
Allow the compiler to accept otherwise illegal code without issuing a diagnostic (for example, a compiler might decide to support a new built-in type long long long
, but any code using that would have to issue a diagnostic; this diagnostic could then be suppressed with e.g. #pragma long long long
.
Allow the compiler to reject otherwise legal code, for example there could be a #pragma strict
which causes the compiler to flag as error the use of certain library functions and/or language constructs which are considered unsafe.
What I actually doubt is allowed, but am not sure either:
for
condition were a postcondition (as in do
… while
), and defined #pragma for postcondion
to switch the meaning of for
accordingly.The reason why I doubt the latter is that the compiler is allowed to ignore any pragma it doesn't recognize, and therefore a change in semantics by a pragma would cause the same program to have different semantics on different compilers.
However, what does the standard actually allow? And are there things which are allowed, but which are not covered by my list above?
Standards allow technology to work seamlessly and establish trust so that markets can operate smoothly. They: provide a common language to measure and evaluate performance, make interoperability of components made by different companies possible, and.
Standards contain technical specifications or other precise criteria designed to be used consistently as a rule, guideline, or definition. They help to make life simpler and increase the reliability and the effectiveness of many of the goods and services we use.
Standards cover a wide range of subjects from construction to nanotechnology, from energy management to health and safety, from baseballs to goalposts. They can be very specific, such as to a particular type of product, or general such as management practices.
Standards also serve to rationalize and reduce production costs, and thus satisfy consumer demands for low-cost services while maintaining good quality. Standards define quality and minimum requirements and provide recognized solutions for the protection of consumers, health, safety and the environment.
The standard is pretty clear on that:
[cpp.pragma] A preprocessing directive of the form
#pragma pp-tokensopt new-line
causes the implementation to behave in an implementation-defined manner. The behavior might cause translation to fail or cause the translator or the resulting program to behave in a non-conforming manner. Any pragma that is not recognized by the implementation is ignored.
The compiler can thus do pretty much whatever it wants on seeing a #pragma
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With