Pigweed
 
Loading...
Searching...
No Matches
pw::ConstexprTag Struct Reference

#include <constexpr_tag.h>

Detailed Description

Tag type used to differentiate between constexpr and non-constexpr constructors. Do NOT use this feature for new classes! It should only be used to add a constexpr constructor to an existing class in limited circumstances.

Specifically, some compilers are more likely to constant initialize global variables that have constexpr constructors. For large non-zero objects, this can increase binary size compared to runtime initialization. Non-zero constant initialized globals are typically placed in .data or .rodata instead of .bss.

Adding constexpr to a constructor may affect existing users if their compiler constant initializes globals that were runtime initialized previously. To maintain previous behavior, add a new constexpr constructor with ConstexprTag instead of changing the existing constructor.

Prefer using pw::kConstexpr to select a constexpr-tagged constructor, rather than initializing a pw::ConstexprTag.

Warning
Do NOT rely on whether a constructor is constexpr or not to control whether global variables are constant initialized. To control constant initialization, explicitly annotate global variables as constinit / PW_CONSTINIT, constexpr, or pw::RuntimeInitGlobal. Compilers can constant initialize globals that:
  • are not declared constinit or constexpr,
  • do not have a constexpr constructor,
  • or perform non-constexpr actions during construction, such as calling non-constexpr functions or placement new.

The documentation for this struct was generated from the following file: