An attempt to remove the last type from the tuple failed

I am trying to remove the last element of a tuple. It works when I have only one item in the tuple to delete. But when I have more than one, things go wrong. I can’t understand why this is not working. These are the errors I get:

prog.cpp: In the function ' int main():
prog.cpp: 24: 22: error: incomplete type' remove_last<std::tuple<int, int> >used in the naming
specifier prog.cpp: 24: 22: error: incomplete type ' remove_last<std::tuple<int, int> >used in the naming
specifier prog.cpp: 24: 70: error: template argument 1 is not valid

#include <tuple>
#include <type_traits>

template <class T>
struct remove_last;

template <class T>
struct remove_last<std::tuple<T>>
{
    using type = std::tuple<>;
};

template <class... Args, typename T>
struct remove_last<std::tuple<Args..., T>>
{
    using type = std::tuple<Args...>;
};

int main()
{
    std::tuple<int, int> var;

    static_assert(
        std::is_same<remove_last<decltype(var)>::type,
        std::tuple<int>>::value, "Values are not the same"
    );
}

, . , , , . ? , , ?

+5
1

, - - , T, Args....

( , std::tuple<T, Args...>):

template <class T, class... Args>
struct remove_last<std::tuple<T, Args...>>
{
    using type = typename concat_tuple<
        std::tuple<T>,
        typename remove_last<std::tuple<Args...>>::type
        >::type;
};

- concat_tuple :

template<typename, typename>
struct concat_tuple { };

template<typename... Ts, typename... Us>
struct concat_tuple<std::tuple<Ts...>, std::tuple<Us...>>
{
    using type = std::tuple<Ts..., Us...>;
};
+5

All Articles