Skip to content

API muesli util concepts

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

muesli/util/concepts

Defined in header <muesli/util/concepts>

is_variant

struct is_variant : std::false_type

Trait to detect std::variant types

Provides a type trait to check if a type is a std::variant. Useful for compile-time type checking and specialisation.

Related

  • is_variant_v for the value form
  • Inherits from: std::false_type

bool

template<typename T> inline constexpr bool is_variant_v = is_variant<T>::value; /** * @brief Concept for types that can be converted to bool * * Requires types to support explicit conversion to bool, such as: * - std::optional * - std::unique_ptr / std::shared_ptr * - Custom types with operator bool() * * @code * struct Optional

Value template for detecting std::variant types

Convenient boolean constant to check if a type is a std::variant.


HasBoolConversion

concept HasBoolConversion

Concept for types that can be converted to bool

Requires types to support explicit conversion to bool, such as:

Example

struct Optional {
    operator bool() const { return has_value; }
    bool has_value = false;
};

static_assert(HasBoolConversion<Optional>);

HasHasValue

concept HasHasValue

Concept for types with a has_value() method

Requires types to provide a has_value() method that returns a boolean. Used for std::optional-like types.

Example

std::optional<int> opt = 42;
static_assert(HasHasValue<decltype(opt)>);

HasOptionalValueCheck

concept HasOptionalValueCheck

Concept for optional-like types with value checking

Composite concept that checks for either bool conversion OR has_value() method. Covers all common optional/nullable types:


HasValueType

concept HasValueType

Concept for types with a value_type member

Requires types to define a value_type member type. Typically used for container and codec types.


HasNextCodec

concept HasNextCodec

Concept for codecs with a next_codec() method

Core requirement for codec types - they must provide a next_codec() method that returns the next codec in the encoding chain.


CanEncodeRvalue

concept CanEncodeRvalue

Concept for codecs that can encode rvalue references

Requires codecs to accept an rvalue reference to their value_type and return the encoded type as an rvalue reference.

Example

template<typename C>
requires CanEncodeRvalue<C>
void encode_temp(const C& codec) {
    typename C::value_type temp = ...;
    auto encoded = codec.encode(std::move(temp));
}

CanEncodeConstRef

concept CanEncodeConstRef

Concept for codecs that can encode const references

Requires codecs to accept a const reference to their value_type and return the encoded type.


CanDecodeRvalue

concept CanDecodeRvalue

Concept for codecs that can decode rvalue references

Requires codecs to accept an rvalue reference to their encoded_type and return the decoded value_type as an rvalue reference.


CanDecodeConstRef

concept CanDecodeConstRef

Concept for codecs that can decode const references

Requires codecs to accept a const reference to their encoded_type and return the decoded value_type.


Codec

concept Codec

Main Codec concept - defines the encoding/decoding interface

The core concept that all codecs must match. Requires:

  • A value_type member for the type being encoded/decoded
  • A next_codec() method that returns the next codec in the chain
  • Ability to encode and decode values (either as rvalues or const references)

This is the foundation for the entire muesli codec framework. Any type satisfying this concept can be used with all codec composition operations.

Example

// All of these satisfy the Codec concept:
identity_codec<int>
array_codec<identity_codec<int>, 3>
optional_codec<identity_codec<int>>
tuple_codec<identity_codec<int>, identity_codec<float>>

static_assert(Codec<identity_codec<int>>);

Related


HasProjectToEncoded

concept HasProjectToEncoded

Concept for types with a project() method that returns an encoded type

Used by projector_codec to check if a type can project a value to the encoded representation.


HasProjectFromOwner

concept HasProjectFromOwner

Concept for types with a project() method given an owner type

Checks if a codec can project from an owner type. Used by tuple_codec to verify all elements support projection.


has_tuple_get_range

template<T>
struct has_tuple_get_range

Helper trait for checking tuple element access

Recursively checks if all elements of a tuple can be accessed via std::get. Used to verify tuple types are valid for tuple_codec.

Related


HasTupleGetUpTo

concept HasTupleGetUpTo

Concept for tuples that support get<0> through get


HasNulloptConstructible

concept HasNulloptConstructible

Concept for types constructible from std::nullopt


is_constructible_from_all_variant_alternatives

template<T, Variant>
struct is_constructible_from_all_variant_alternatives

Trait to check if type is constructible from all variant alternatives


ConstructibleFromAllVariantAlternatives

concept ConstructibleFromAllVariantAlternatives

Concept for types constructible from all alternatives in a variant


HasTupleSize

concept HasTupleSize

Concept that detects presence of std::tuple_size specialisation

Satisfied for tuple-like types (std::tuple, std::pair, etc.). Avoids instantiating tuple_size_v on non-tuples.


Clone this wiki locally