我尝试将模板参数列表(枚举类,而不是类型名)转换为模板参数的相应类型名,然后转换为std::tuple。我猜如果我能以某种方式将变量列表命名为using type = ...
,我可能能够在下一个模板递归中扩展它
我有一个enum类:
enum class object_t{
STR,
INT,
FLOAT,
};
模板应该提供具体的类(一些std::tuple):
template <object_t _obj, object_t ...Args>
struct concrete {
// here's the part I need to correct
using type = std::tuple<typename concrete<_obj>::type, typename concrete<Args...>::type>;
};
专门化来结束递归:
template <>
struct concrete<object_t::STR> {
using type = std::string;
};
template <>
struct concrete<object_t::INT> {
using type = int64_t;
};
template <>
struct concrete<object_t::FLOAT> {
using type = double;
};
类型简写的using声明
template<object_t _obj, object_t ...Args>
using concrete_t = typename concrete<_obj, Args...>::type;
最终,我想要的是
concrete_t<object_t::INT, object_t::FLOAT, object_t::STR>
等同于
std::tuple<int64_t, double, std::string>
目前,这应该产生如下内容:
std::tuple<int64_t, std::tuple<double, std::string>>
而不是.
我不是最好的可变模板,但我想如果使用类型(通用模板)是一个参数包而不是一个元组,我可能能够解包它为下一个元组(其参数列表,我将不得不再次获得等等)。例如:
template <object_t _obj, object_t ...Args>
struct concrete {
using type = std::tuple<typename concrete<_obj>::type, typename concrete<Args...>::type...>::elements_type;
};
其中elements_type是一个变量包,而::type.打开它
但即使这样也似乎不对,因为root::type将是一个参数包,而不是像desired那样的std::tuple。也许需要另一个模板,我不知道。
任何建议都可能会有很长的路要走,谢谢!
1条答案
按热度按时间webghufk1#
如果有人需要这个,解决方案,感谢@IgorTandetnik,看起来像这样:
这个答案是基于@IgorTandetnik评论。
为了在单个模板参数的普通情况下消除std::tuple,
例如,使
concrete_t<object_t::STR>
成为std::string
而不是std::tuple<std::string>
。