别名模板参数包 [英] Aliasing a template parameter pack
问题描述
剧情
也许这个问题是重复的,但是我对模板编程还是比较陌生,实际上我找不到简单和简短解决方案(->仅找到兆字节的" roxxor-template-magic "(我不理解)匹配我的具体和简单问题,因此我现在有点迷失了.
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.
预信息
我想使用"type_container"作为类的模板参数.容器是一个简单的结构,其中还应包含一些用于模板参数包的 typedefs .
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.
问题:(与底部的示例有关)
如何在容器结构中定义和命名类型列表,以及如何访问它们并将其转发到工厂类中所需的实体? (没有提升,只有C ++ 11/14/17 )
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)
示例:(如果需要,可以编译)
#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;
}
当前(最佳?)精心设计的解决方案(可编译)(基于John Zwinck的回答)
#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;
}
推荐答案
您实际上可以使用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);
也许:
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!
这篇关于别名模板参数包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!