Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the standard allow #pragma to do?

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:

  • Allow to select one of several compilation options which all result in valid C++- For example, select one of several available ABIs, or switch certain implementation defined options.

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:

  • Allow the compiler to change the semantics of legal code to something different (for example, assume that a compiler vendor considered it a good idea if the for condition were a postcondition (as in dowhile), 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?

like image 823
celtschk Avatar asked Jul 07 '13 10:07

celtschk


People also ask

What do standards enable?

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.

What is the purpose of a standard?

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.

What do standards cover?

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.

What are the benefits of standard?

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.


1 Answers

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.

like image 155
filmor Avatar answered Oct 17 '22 18:10

filmor