Skip to content

Commit 8c00563

Browse files
TylerMSFTTylerMSFT
andauthored
start draft (#3248)
* start draft * links * link * acrolinx and link * tech review * comment * link broken by heading change * tech review * shortened up the trailing slashes Co-authored-by: TylerMSFT <[email protected]>
1 parent 38cffd5 commit 8c00563

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

docs/build/reference/std-specify-language-standard-version.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "/std (Specify Language Standard Version)"
33
description: "The MSVC compiler option /std specifies the C or C++ language standard supported by the compiler."
4-
ms.date: 09/11/2020
4+
ms.date: 10/29/2020
55
f1_keywords: ["/std", "-std", "/std:c++14", "/std:c++17", "/std:c11", "/std:c17", "VC.Project.VCCLCompilerTool.CppLanguageStandard"]
66
ms.assetid: 0acb74ba-1aa8-4c05-b96c-682988dc19bd
77
---
@@ -64,7 +64,7 @@ Starting in Visual Studio 2019 version 16.8, you may specify **`/std:c11`** or *
6464
6565
When you specify **`/std:c11`** or **`/std:c17`**, MSVC supports all the required features of C11 and C17. The compiler options enable support for these functionalities:
6666

67-
- **`_Pragma`**
67+
- [`_Pragma`](../../preprocessor/pragma-directives-and-the-pragma-keyword.md#the-_pragma-preprocessing-operator-c99-c11)
6868

6969
- **`restrict`**
7070

docs/preprocessor/pragma-directives-and-the-pragma-keyword.md

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: "Pragma directives and the __pragma keyword"
3-
ms.date: "08/29/2019"
3+
description: "Describes the pragma directives available in Microsoft Visual C and C++ (MSVC)"
4+
ms.date: "10/30/2020"
45
f1_keywords: ["#pragma"]
56
helpviewer_keywords: ["#pragma directives, C/C++", "__pragma keyword", "pragma directives, C/C++", "pragmas, C/C++", "preprocessor", "pragmas", "preprocessor, pragmas", "pragma directives (#pragma)"]
67
ms.assetid: 9867b438-ac64-4e10-973f-c3955209873f
@@ -11,16 +12,19 @@ Pragma directives specify machine- or operating system-specific compiler feature
1112

1213
## Syntax
1314

14-
> **#pragma** *token-string*\
15-
> **__pragma(** *token-string* **)**
15+
> **#`pragma`** *token-string*\
16+
> **`__pragma(`** *token-string* **`)`** // two leading underscores - Microsoft specific extension
17+
> **`_Pragma(`** *string-literal* **`)`** // C99
1618
1719
## Remarks
1820

1921
Each implementation of C and C++ supports some features unique to its host machine or operating system. Some programs, for example, must exercise precise control over the location of data in memory, or control the way certain functions receive parameters. The **#pragma** directives offer a way for each compiler to offer machine- and operating system-specific features, while maintaining overall compatibility with the C and C++ languages.
2022

2123
Pragmas are machine- or operating system-specific by definition, and are typically different for every compiler. Pragmas can be used in conditional directives, to provide new preprocessor functionality, or to provide implementation-defined information to the compiler.
2224

23-
The *token-string* is a series of characters that gives a specific compiler instruction and arguments, if any. The number sign (**#**) must be the first non-white-space character on the line that contains the pragma. White-space characters can separate the number sign and the word "pragma". Following **#pragma**, write any text that the translator can parse as preprocessing tokens. The argument to **#pragma** is subject to macro expansion.
25+
The *token-string* is a series of characters representing a specific compiler instruction and arguments, if any. The number sign (**#**) must be the first non-white-space character on the line that contains the pragma. White-space characters can separate the number sign and the word "pragma". Following **#pragma**, write any text that the translator can parse as preprocessing tokens. The argument to **#pragma** is subject to macro expansion.
26+
27+
The *string-literal* is the input to `_Pragma`. Outer quotes and leading/trailing whitespace are removed. `\"` is replaced with `"` and `\\` is replaced with `\`.
2428

2529
The compiler issues a warning when it finds a pragma that it doesn't recognize, and continues compilation.
2630

@@ -99,9 +103,9 @@ cl /Zp8 some_file.cpp
99103

100104
## The __pragma() keyword
101105

102-
The compiler also supports the Microsoft-specific **__pragma** keyword, which has the same functionality as the **#pragma** directive. The difference is, the **__pragma** keyword is usable inline in a macro definition. The **#pragma** directive isn't usable in a macro definition, because the compiler interprets the number sign character ('#') in the directive as the [stringizing operator (#)](../preprocessor/stringizing-operator-hash.md).
106+
The compiler also supports the Microsoft-specific **`__pragma`** keyword, which has the same functionality as the **`#pragma`** directive. The difference is, the **`__pragma`** keyword is usable inline in a macro definition. The **`#pragma`** directive isn't usable in a macro definition, because the compiler interprets the number sign character ('#') in the directive as the [stringizing operator (#)](../preprocessor/stringizing-operator-hash.md).
103107

104-
The following code example demonstrates how the **__pragma** keyword can be used in a macro. This code is excerpted from the mfcdual.h header in the ACDUAL sample in "Compiler COM Support Samples":
108+
The following code example demonstrates how the **`__pragma`** keyword can be used in a macro. This code is excerpted from the *mfcdual.h* header in the ACDUAL sample in "Compiler COM Support Samples":
105109

106110
```cpp
107111
#define CATCH_ALL_DUAL \
@@ -121,6 +125,48 @@ END_CATCH_ALL \
121125
return _hr; \
122126
```
123127
128+
## The `_Pragma` preprocessing operator (C99, C++11)
129+
130+
`_Pragma` is similar to the Microsoft-specific [`__pragma`](#the-__pragma-keyword) keyword, except it's part of the standard. It was introduced for C in C99. For C++, it was introduced in C++11.
131+
132+
It allows you to put pragmas into a macro definition. It has one leading underscore `_` instead of two leading underscores `__` that the Microsoft-specific keyword has, and the first letter is capitalized.
133+
134+
The string literal should be what you would otherwise put following a *`#pragma`* statement. For example:
135+
136+
```c
137+
#pragma message("--the #pragma way")
138+
_Pragma ("message( \"the _Pragma way\")")
139+
```
140+
141+
Quotation marks and back-slashes should be escaped, as shown above. A pragma string that isn't recognized is ignored.
142+
143+
The following code example demonstrates how the **`_Pragma`** keyword could be used in an assert-like macro when you don't want to get a warning when the condition expression happens to be constant.
144+
145+
The macro definition uses the do/while(0) idiom for multi-statement macros so that it can be used as though it were one statement. See [C multi-line macro](https://stackoverflow.com/questions/1067226/c-multi-line-macro-do-while0-vs-scope-block) on Stack Overflow for more info. The _Pragma statement only applies to the line of code that follows it.
146+
147+
```C
148+
// Compile with /W4
149+
150+
#include <stdio.h>
151+
#include <stdlib.h>
152+
153+
#define MY_ASSERT(BOOL_EXPRESSION) \
154+
do { \
155+
_Pragma("warning(suppress: 4127)") /* C4127 conditional expression is constant */ \
156+
if (!(BOOL_EXPRESSION)) { \
157+
printf("MY_ASSERT FAILED: \"" #BOOL_EXPRESSION "\" on %s(%d)", __FILE__, __LINE__); \
158+
exit(-1); \
159+
} \
160+
} while (0)
161+
162+
int main()
163+
{
164+
MY_ASSERT(0 && "Note that there is no warning: C4127 conditional expression is constant");
165+
166+
return 0;
167+
}
168+
```
169+
124170
## See also
125171

126172
[C/C++ preprocessor reference](../preprocessor/c-cpp-preprocessor-reference.md)\

0 commit comments

Comments
 (0)