Aliasing a template parameter pack

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

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!


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

链接地址: http://www.djcxy.com/p/82546.html

上一篇: 检测Android设备中的HDMI端口

下一篇: 别名模板参数包