Is std::variant allowed to allocate memory for its members?

I was wondering if an implementation of std::variant must necessarily be "flat" or whether it is allowed to dynamically allocate memory for its members, such that a sequence of variants would degenerate to a sequence of pointers, thereby destroying cache locality.

Answers


No, very explicitly. From [variant.variant]:

Any instance of variant at any given time either holds a value of one of its alternative types, or it holds no value. When an instance of variant holds a value of alternative type T, it means that a value of type T, referred to as the variant object's contained value, is allocated within the storage of the variant object. Implementations are not permitted to use additional storage, such as dynamic memory, to allocate the contained value. The contained value shall be allocated in a region of the variant storage suitably aligned for all types in Types.... It is implementation-defined whether over-aligned types are supported.


According to cppreference ::std::variant must not allocate dynamic memory.

As with unions, if a variant holds a value of some object type T, the object representation of T is allocated directly within the object representation of the variant itself. Variant is not allowed to allocate additional (dynamic) memory.


Need Your Help

Reading GHC Core

performance compiler-construction haskell ghc

Core is GHC's intermediate language. Reading Core can help you better understand the performance of your program. Someone asked me for documentation or tutorials on reading Core, but I couldn't fin...

IntelliJ IDEA: How to rename a project (in "Recent Projects" list)?

intellij-idea

Is there an easy way in IDEA 12 to rename a project in the "Recent Projects" list?