2

Pre-Dramatic

Hi, maybe this question is a duplicate, but I am relative new to template programming and actually I am not able to find a simple and short solution (-> only finding megabytes of "roxxor-template-magic" which I don't understand) matching my concrete and simple problem and so I feel a little bit lost now.

Pre-Information

I want to use a "type_container" as template parameter for a class. The container is a simple struct, which should also contain some typedefs for template parameter packs.

Question: (related to the example at the bottom)

How can I define and alias the type-lists in the container-struct and how can I access and forward them to the needed entities in the factory-class? (No boost and only C++11/14/17)

Example: (Would compile, If I would know a solution)

#include <map>
#include <vector>
#include <string>
#include <stack>

struct MY_TYPE_CONTAINER
{
    using TYPE_MAP            = std::map<int, int>;
    using TYPE_VECTOR         = std::vector<double>;
    using PARAMETER_PACK1 = //? -> ...T -> int, std::stack<int>, int
    using PARAMETER_PACK2 = //? -> ...T -> double, int, std::string
};

template <typename TYPES>
struct Factory
{
    typename TYPES::TYPE_MAP                       m_map;
    typename TYPES::TYPE_VECTOR                    m_vector;
    typename std::tuple<TYPES::PARAMETER_PACK1...> m_tuple;
    typename std::tuple<std::shared_ptr<TYPES::PARAMETER_PACK1>...> m_tuple2;
    void handlePack2(TYPES::PARAMETER_PACK2... args) { }
};


int main()
{
    Factory<MY_TYPE_CONTAINER> oStaticFactory;
    oStaticFactory.handlePack2(2.0, 1, "hi");
    return 0;
}

Current (best?) elaborated solution (compilable) (Based on John Zwinck's answer)

#include <map>
#include <vector>
#include <string>
#include <stack>
#include <memory>

struct MY_TYPE_CONTAINER
{
    using TYPE_MAP                    = std::map<int, int>;
    using TYPE_VECTOR                 = std::vector<double>;
    using PARAMETER_PACK1             = std::tuple<int, std::stack<int>, int>;
    using PARAMETER_SHARED_PTR_PACK_1 = std::tuple<std::shared_ptr<int>, std::shared_ptr<std::stack<int>>, std::shared_ptr<int>>;
    using PARAMETER_PACK2             = std::tuple<double, int, std::string>;
};

template <typename TYPES>
class Factory
{
    typename TYPES::TYPE_MAP                    m_map;
    typename TYPES::TYPE_VECTOR                 m_vector;
    typename TYPES::PARAMETER_PACK1             m_tuple;
    typename TYPES::PARAMETER_SHARED_PTR_PACK_1 m_tuple2;

    void handlePack2Impl(typename TYPES::PARAMETER_PACK2 tup) {};

public:
    template <typename... Args>
    void handlePack2(Args... args) { handlePack2Impl(std::make_tuple(args...)); }
};

int main()
{
    Factory<MY_TYPE_CONTAINER> oStaticFactory;
    oStaticFactory.handlePack2(2.0, 1, "hi");
    return 0;
}
SoulfreezerXP
  • 459
  • 1
  • 5
  • 19

2 Answers2

2

You can actually use tuple:

using PARAMETER_PACK1 = std::tuple<int, std::stack, int>;
using PARAMETER_PACK2 = std::tuple<double, int, std::string>;

typename TYPES::PARAMETER_PACK1 m_tuple;

Then you're left with one problem--how to declare something like this:

void handlePack2(TYPES::PARAMETER_PACK2... args);

Perhaps:

void handlePack2Impl(typename TYPES::PARAMETER_PACK2 tup);

template <typename... Args>
void handlePack2(Args... args) { handlePack2Impl(std::make_tuple(args...)); }

For more ideas, start here: Tuple to parameter pack (but beware, there is template meta-programming!

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 2
    C++17 adds [`std::apply`](http://en.cppreference.com/w/cpp/utility/apply) which will also do the work of expanding a tuple to a series of parameters. – Drew Dormann Dec 23 '17 at 00:45
  • 1
    @DrewDormann: we are converting a set of arguments to a tuple, not the other way around. I'm not sure how `std::apply` applies. – John Zwinck Dec 23 '17 at 07:18
  • @John Zwinck: That means, I cannot avoid to specify those member-types of the factory in the type-container? I extended the code with another member m_tuple2. How can I handle this type of member? Should I transfer this full-type also to the type-container? – SoulfreezerXP Dec 23 '17 at 11:17
  • Added your solution to my question, if that is allowed here?! – SoulfreezerXP Dec 23 '17 at 12:23
1

Basically you can't do that. The closest thing you can do is to save std::tuple<type, type, type> (or your own variadic_class<type, type, type> which may have empty body) and then retrieving the list back by little templates magic

RiaD
  • 46,822
  • 11
  • 79
  • 123