TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
 
C++ Coding Standard
Search

Rule:  PCA#010Checked automatically with code checker

Synopsis:Use variants instead of unions
Language:C++
Severity Level:6
Category:Parts of C++ to Avoid


Description:

Variants are type-safe unions. They are available since C++17. An instance of std::variant at any given time either holds a value of one of its alternative types, or in the case of error - no value. The latter state is hard to achieve.

Example (wrong):

union SuperFloat
{
    float f;
    int i;
};

Example (right):

using std::variant SuperFloat;