Edit

Share via


Warning C6389

Move 'declaration' to anonymous namespace or put a forward declaration in a common header included in this file.

Remarks

This check is intended to help reduce the visibility of certain symbols and to modularize the code. In multi-file C++ projects, each declaration should be either local to a C++ file (part of the anonymous namespace) or declared in a common header file that's included by multiple C++ files.

When this check flags a declaration, either it should be moved to an anonymous namespace or a forward declaration should be moved to a header file, depending on the scope of the symbol.

The rule is an experimental rule that must be explicitly enabled in a rule set file to work. For more information about rule sets, see Use rule sets to group code analysis rules.

Code analysis name: MARK_INTERNAL_OR_MISSING_COMMON_DECL

Example

// A.h
struct X;

// A.cpp
#include "A.h"

// Not flagged, declared in a header file.
struct X { int x; };

struct Y { double y; }; // warning: Move 'Y' to anonymous namespace or put a forward declaration in a common header included in this file.

void f();  // warning: Move 'f' to anonymous namespace or put a forward declaration in a common header included in this file.

One way to resolve these issues is to move struct Y into an anonymous namespace, and move the declaration of f into a header:

// A.h
struct X;
void f();

// A.cpp
#include "A.h"

// Not flagged, declared in a header file.
struct X { int x; };

namespace {
  struct Y { double y; };
} // anonymous namespace

// Not flagged, declared in a header file.
void f();