C++ how to replace type in variadic template by its index

How can I replace 1 type from variadic template by its index? For example I have:

//-------------static_list-------------
template<class ...Elements>
struct static_list {};

struct execute_command<index, static_list<Commands...>> {
    static constexpr int value = execute_s<index + 1, static_list<Commands...>>::value;
}

and I want to rewrite it so in Commands it will replace type at for example index 5 with type variable<0, 0> like this:

struct execute_command<false, true, false, false, false, index, static_list<Commands...>> {
    static constexpr int value = execute_s<index + 1, static_list_replace<5, variable<0, 0>, Commands...>>::value;
}

I have tried it many different ways but nothing was working. For example:

//-------------static_list_replace-------------
template<std::size_t N, typename T, typename Tuple, class IndexSequence>
struct static_list_replace;

template<std::size_t N, typename T, typename Tuple, std::size_t... Idx>
struct static_list_replace<N, T, Tuple, std::index_sequence<Idx ...>> {
    using type = std::tuple<std::conditional_t<N == Idx, T, std::tuple_element_t<N, Tuple>>...>;
};

or

namespace detail {
    template<std::size_t N, typename T, typename Tuple, std::size_t... Idx>
    auto replace(std::index_sequence<Idx...>) ->
        std::tuple<std::conditional_t<N == Idx,
        T,
        std::tuple_element_t<N, Tuple>>...>;
}

template <std::size_t N, typename T, typename... Args>
using Replace = decltype(detail::replace<N, T, std::tuple<Args...>>
    (std::index_sequence_for<Args...>{}));

or I tried to get beginnig of the list and end without the one element and then pack it like static_list<list_beginning<index, Commands...>, element, list_ending<index, Commands...>>>

Nothing worked.



Read more here: https://stackoverflow.com/questions/67389581/c-how-to-replace-type-in-variadic-template-by-its-index

Content Attribution

This content was originally published by Rikib161999 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: