Skip to content

Latest commit

 

History

History
53 lines (40 loc) · 1.64 KB

File metadata and controls

53 lines (40 loc) · 1.64 KB
description Learn more about: C26427 NO_GLOBAL_INIT_EXTERNS
title C26427
ms.date 11/15/2017
ms.topic conceptual
f1_keywords
C26427
helpviewer_keywords
C26427
ms.assetid 8fb95a44-8704-45b1-bc55-eccd59b1db2f

C26427 NO_GLOBAL_INIT_EXTERNS

"Global initializer accesses extern object."

C++ Core Guidelines: I.22: Avoid complex initialization of global objects

Global objects may be initialized in an inconsistent or undefined order, which means that interdependency between them is risky and should be avoided. This guideline is applicable when initializers refer to another object that's considered to be extern.

Remarks

An object is deemed extern if it conforms to the following rules:

  • it's a global variable marked with extern specifier or it is a static member of a class;
  • it's not in an anonymous namespace;
  • it's not marked as const;
  • Static class members are considered global, so their initializers are also checked.

Example

external version check

// api.cpp
int api_version = API_DEFAULT_VERSION; // Assume it can change at run time, hence non-const.

// client.cpp
extern int api_version;
bool is_legacy_mode = api_version <= API_LEGACY_VERSION; // C26427, also stale value

external version check – made more reliable

// api.cpp
int api_version = API_DEFAULT_VERSION; // Assume it can change at run time, hence non-const.

// client.cpp
extern int api_version;
bool is_legacy_mode() noexcept
{
    return api_version <= API_LEGACY_VERSION;
}