Skip to content

Commit adbab11

Browse files
authored
Merge pull request #2999 from corob-msft/docs/corob/cpp-docs-2313-C5208
Address 2313 Add warning C5208
2 parents ea0ebb9 + 16a62dc commit adbab11

File tree

4 files changed

+369
-200
lines changed

4 files changed

+369
-200
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
title: "Compiler Warning C5208, Error C7626"
3+
description: Compiler warning C5208 description and solution.
4+
ms.date: 07/17/2020
5+
f1_keywords: ["C5208", "C7626"]
6+
helpviewer_keywords: ["C5208", "C7626"]
7+
---
8+
# Compiler Warning (level 1) C5208 and Error C7626
9+
10+
> unnamed class used in typedef name cannot declare members other than non-static data members, member enumerations, or member classes
11+
12+
## Remarks
13+
14+
Unnamed classes within a **`typedef`** declaration can't have any members other than:
15+
16+
- non-static data members,
17+
- member classes,
18+
- member enumerations,
19+
- and default member initializers.
20+
21+
The same restrictions are applied recursively to each nested class. The restriction is meant to ensure the simplicity of structs that have `typedef` names for linkage purposes. They must be simple enough that no linkage calculations are necessary before the compiler gets to the `typedef` name for linkage.
22+
23+
This warning is new in Visual Studio 2019 version 16.6. Based on [P1766R1](https://wg21.link/P1766R1) adopted by the C++ standards committee as a defect report, it affects all standards modes of the compiler. In default **`/std:c++14`** and **`/std:c++17`** modes, the compiler emits warning C5208 for non-conforming code. If **`/permissive-`** is specified, the compiler emits warning C5208 as an error under **`/std:c++14`** and emits error C7626 under **`/std:c++17`**. The compiler emits error C7626 for non-conforming code when **`/std:c++latest`** is specified.
24+
25+
### To turn off the warning without code changes
26+
27+
You can turn off the warning for a specific line of code by using the [warning](../../preprocessor/warning.md) pragma, `#pragma warning(suppress : 5208)`. You can also turn off the warning within a file by using the warning pragma, `#pragma warning(disable : 5208)`. You can turn off the warning globally in command-line builds by using the **/wd5208** command-line option.
28+
29+
To turn off the warning for an entire project in the Visual Studio IDE:
30+
31+
1. Open the **Property Pages** dialog for your project. For information on how to use the Property Pages dialog, see [Property Pages](../../build/reference/property-pages-visual-cpp.md).
32+
1. Select the **Configuration Properties** > **C/C++** > **Advanced** page.
33+
1. Edit the **Disable Specific Warnings** property to add *`5208`*. Choose **OK** to apply your changes.
34+
35+
## Example
36+
37+
The following sample shows the constructs that are no longer allowed in unnamed structs. Depending on the standards mode specified, C5208 or C7626 errors or warnings are emitted:
38+
39+
```cpp
40+
struct Base { };
41+
typedef struct : Base // inheriting from 'Base'; ill-formed
42+
{
43+
void fn(); // ill-formed
44+
static int i; // ill-formed
45+
struct U {
46+
void f(); // nested class has non-data member; ill-formed
47+
};
48+
int j = 10; // default member initializer; ill-formed
49+
} S;
50+
```
51+
52+
The code above can be fixed by giving the unnamed class a name:
53+
54+
```cpp
55+
struct Base { };
56+
typedef struct NamedType : Base
57+
{
58+
void fn();
59+
static int i;
60+
struct U {
61+
void f();
62+
};
63+
int j = 10;
64+
} S;
65+
```

0 commit comments

Comments
 (0)