Login
You're viewing the code4lib.social public feed.
  • Nov 26, 2026, 2:05 PM

    I think that yes this is generally the correct way to build the version-dependent default configuration value store in C++

    I leave it up to the reader to marray a get_all<FixedString> with a runtime selector based on the version. Optimally you build this into a decent lookup tree at compile time.

    template <std::size_t N>
struct FixedString {
    char data[N]{};
    constexpr FixedString(const char (&str)[N]) { std::copy_n(str, N, data); }
    constexpr operator std::string_view() const { return {data, N - 1}; }
};

template <std::size_t NameLen, typename T, std::size_t TrackCount = 0>
struct option {
    FixedString<NameLen> name;
    T value;
    std::pair<int, int> version{0, 0};
    std::array<int, TrackCount> tracks{};
};

// 1. Deduction guide when `tracks` is passed as std::tuple{...}
template <std::size_t NameLen, typename T, typename... Tracks>
option(const char (&)[NameLen], T, std::pair<int, int>, std::tuple<Tracks...>)
    -> option<NameLen, T, sizeof...(Tracks)>;

// 2. Deduction guide when `tracks` is passed as a braced array/list {0, 1, 2, 3}
template <std::size_t NameLen, typename T, std::size_t TrackCount>
option(const char (&)[NameLen], T, std::pair<int, int>, const int (&)[TrackCount])
    -> option<NameLen, T, TrackCount>;

// 3. Deduction guide when `version` and `tracks` are omitted (e.g. "APT::VeryOld")
template <std::size_t NameLen, typename T>
option(const char (&)[NameLen], T)
    -> option<NameLen, T, 0>;

constexpr auto features = std::tuple{
    option{"APT::Solver", "internal", {3, 1}, {0, 1, 2, 3}},
    option{"APT::Solver", "3.0", {3, 1}, {0, 1, 2, 3}},
    option{"APT::Solver::StrictPinning", true, {3, 1},{0, 1, 2, 3}},
    option{"APT::Output-Version", 30, {3, 0},{3}},
    option{"APT::VeryOld", 000},
};
    💬 0🔄 0⭐ 0