Skip to content

API muesli variant_codec

Felix Jones edited this page Jan 15, 2026 · 1 revision

muesli/variant_codec

Defined in header <muesli/variant_codec>

variant_codec

struct variant_codec : fluent::optionable<variant_codec<Codecs...>>,
                       fluent::constrainable<variant_codec<Codecs...>>,
                       fluent::transformable<variant_codec<Codecs...>>,
                       fluent::projectable<variant_codec<Codecs...>>,
                       fluent::constructable<variant_codec<Codecs...>>,
                       fluent::nullableable<variant_codec<Codecs...>>

Codec for std::variant types.

The variant_codec encodes/decodes std::variant<Ts...> using a discriminant (variant index) followed by the encoded value for the active alternative. This enables polymorphic serialisation with compact storage.

Template Parameters

Parameter Description
Codecs Variadic list of codecs (one per variant alternative) Perfect for: - Message types (command, response, error) - Polymorphic data (int or string or floating point) - State machines (idle, running, paused, done) - Result types (success or error)

Member Types

value_type

using value_type = std::variant<typename Codecs::value_type...>

Value type is a variant of all codec value types

Member Functions

codecs

Constructs a variant_codec from the given alternative codecs

explicit constexpr variant_codec(const Codecs&... cs) noexcept : codecs(cs...)

Notes

Discriminant is encoded as uint8_t, limiting variants to ≤256 alternatives

Example

// Message type: either an integer ID or string name
auto msg_codec = variant_codec(int32_codec, string_codec);
std::variant<int32_t, std::string> msg1 = 42;         // int alternative
std::variant<int32_t, std::string> msg2 = "alert";    // string alternative
auto encoded1 = msg_codec.encode(msg1);
auto encoded2 = msg_codec.encode(msg2);

// Response type: success with data or error with message
auto response = variant_codec(
    tuple_codec(int32_codec, string_codec),  // success: (status_code, data)
    string_codec                              // error: (error message)
);
using Response = std::variant<
    std::tuple<int32_t, std::string>,  // success: code and data
    std::string                        // error: message only
>;
Response ok = std::tuple<int32_t, std::string>{200, "OK"};
Response err = std::string{"Not found"};
auto encoded_ok = response.encode(ok);
auto encoded_err = response.encode(err);

// State machine: idle, running with progress, or done with result
enum class State { Idle, Running, Done };
auto state = variant_codec(
    monostate_codec,        // Idle state (no data)
    int32_codec,            // Running state (progress 0-100)
    string_codec            // Done state (result message)
);
using StateValue = std::variant<std::monostate, int32_t, std::string>;

Related


codecs

using value_type = std::variant<typename Codecs::value_type...>; /** @brief Tuple of codecs, one for each variant alternative */ std::tuple<Codecs...> codecs; /** @brief Constructs a variant_codec from the given alternative codecs */ explicit constexpr variant_codec(const Codecs&... cs) noexcept : codecs(cs...)

Value type is a variant of all codec value types


next_codec

constexpr auto next_codec() const noexcept

Returns a variant_codec of the next codecs in the chain


encode

constexpr auto encode(const value_type& value) const noexcept

Encodes a variant by encoding the active alternative


decode

constexpr value_type decode(auto&& value) const noexcept

Decodes an encoded variant by decoding the appropriate alternative


Clone this wiki locally