Search code examples
c++stdstdoptional

Are std::optional members stored contiguously?


I suppose I'm a bit confused as to how exactly optional values are stored. When constructing a class or struct that contains std::optional<T> members, will these members be stored contiguously in memory or does optional allocate dynamically? For example, would the below struct be one contiguous block of memory?

struct Material
    {
        std::string name;
        std::optional<size_t> albedo;
        std::optional<size_t> normal;
        std::optional<size_t> metalness;
        std::optional<size_t> roughness;
        std::optional<size_t> ao; // ambient occlusion
        bool hasAlphaChannel = false;
    };

Solution

  • optional is required to not use dynamic allocation.

    If an optional contains a value, the value is guaranteed to be allocated as part of the optional object footprint, i.e. no dynamic memory allocation ever takes place. Thus, an optional object models an object, not a pointer, even though operator*() and operator->() are defined.

    https://en.cppreference.com/w/cpp/utility/optional

    There is also the member variables of optional and also padding that can occur. so no, they are not necessarily contiguous, but they are within the object you declare them in.