Редагувати

Поділитися через


Compiler Warning (level 1) C5208 and Error C7626

unnamed class used in typedef name cannot declare members other than non-static data members, member enumerations, or member classes

Remarks

Unnamed classes within a typedef declaration can't have any members other than:

  • non-static data members with no default member initializers,
  • member classes, or
  • member enumerations.

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.

This warning is new in Visual Studio 2019 version 16.6. Based on 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. The compiler emits error C7626 for non-conforming code when /std:c++17 or later is specified.

To turn off the warning without code changes

You can turn off the warning for a specific line of code by using the warning 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.

To turn off the warning for an entire project in the Visual Studio IDE:

  1. Open the Property Pages dialog for your project. For information on how to use the Property Pages dialog, see Property Pages.
  2. Select the Configuration Properties > C/C++ > Advanced page.
  3. Edit the Disable Specific Warnings property to add 5208. Choose OK to apply your changes.

Example

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:

struct Base { };
typedef struct : Base // inheriting from 'Base'; ill-formed
{
    void fn(); // ill-formed
    static int i; // ill-formed
    struct U {
        void f(); // nested class has non-data member; ill-formed
    };
    int j = 10; // default member initializer; ill-formed
} S;

The code above can be fixed by giving the unnamed class a name:

struct Base { };
typedef struct NamedType : Base
{
    void fn();
    static int i;
    struct U {
        void f();
    };
    int j = 10;
} S;