Stefano
Stefano

Reputation: 3258

Unpack all variadic template arguments except last one as type of variable

I have a template class that accepts a variable number of types as arguments. The constructor accepts a pointer to an instance of the class that uses Args... - 1 as parameter type. Searching over the internet I found out that a std::tuple is often used to handle this type of problems, but I don't understand how I could take the template arguments, create a tuple, remove the last type and then unpack the tuple again and store the result in a variable that can be retrieved later by the parent() function.

template<typename ...Args>
class MyClass
{
public:
    MyClass(MyClass<Args...> *parent) : parent_(parent) // Should be Args - 1
   {
   }

    MyClass<Args...>* parent()
    {
        return parent_;
    }

private:
    MyClass<Args...> *parent_;
};

I found different answers here on StackOverflow about similar topics that involve tuples. This code has been posted on another question and should get a tuple with all parameters except the last one. The problem is that I don't know how to adapt it to unpack that tuple again.

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...>;
};

template <class T>
struct remove_last;

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

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;
};

Upvotes: 4

Views: 1668

Answers (1)

Piotr Skotnicki
Piotr Skotnicki

Reputation: 48467

#include <type_traits>
#include <tuple>
#include <utility>
#include <cstddef>

template <template <typename...> class C, typename... Args, std::size_t... Is>
auto pop_back(std::index_sequence<Is...>) noexcept
    -> C<std::tuple_element_t<Is, std::tuple<Args...>>...>&&;

template <typename... Args>
class MyClass
{
    using Parent = std::remove_reference_t<
                      decltype(pop_back<::MyClass, Args...>(std::make_index_sequence<sizeof...(Args) - 1>{}))
                   >;

public:    
    explicit MyClass(Parent* parent) : parent_(parent)
    {

    }

    Parent* parent()
    {
        return parent_;
    }

private:
    Parent* parent_;
};

template <>
class MyClass<> {};

int main()
{
    MyClass<> a;
    MyClass<int> b(&a);    
    MyClass<int, char> c(&b);
    MyClass<int, char, float> d(&c);
}

DEMO


The answer for the previous question, before the edit:

#include <tuple>
#include <utility>
#include <cstddef>

template <typename... Args>
class MyClass
{
public:    
    auto newInstance()
    {
        return newInstance(std::make_index_sequence<sizeof...(Args) - 1>{});
    }

private:        
    template <std::size_t... Is>
    MyClass<typename std::tuple_element<Is, std::tuple<Args...>>::type...> newInstance(std::index_sequence<Is...>)
    {
        return {};
    }
};

DEMO 2


Why the pop_back function has no body?

This is actually a trait implemented in terms of a function declaration. Alternatively, you could use a more classic solution with a structure specialization:

template <typename T, typename S>
struct pop_back;

template <template <typename...> class C, typename... Args, std::size_t... Is>
struct pop_back<C<Args...>, std::index_sequence<Is...>>
{
    using type = C<std::tuple_element_t<Is, std::tuple<Args...>>...>;
};

and then use:

using Parent = typename pop_back<MyClass, std::make_index_sequence<sizeof...(Args) - 1>>::type;

That is, I used a function declaration to shorten the syntax. And it doesn't require a body, since noone is supposed to call this function in an evaluated context.

Why are you using noexcept?

Imagine you have a function:

void foo(MyClass<int, char>) noexcept {}

And elsewhere you want to check whether the call is noexcept:

static_assert(noexcept(foo(pop_back<MyClass, int, char, float>(std::index_sequence<0, 1>{}))), "!");

Without the noexcept specifier, the above assertion would fail, since the call to pop_back would be considered as possibly throwing code.

Upvotes: 6

Related Questions