给定以下类模板:
template <template <typename... Args> class Container, typename... Args>
struct container_type_holder {};我想提取它的模板模板参数和可变参数,以便在另一个上下文中重用。示例:
using c1 = container_type_holder<std::map, std::string, int>;
using c2 = container_type_holder<tt_parameter<c1>, vt_parameter<c1>>;其中,tt_parameter<c1>是从c1和vt_parameter<c1>中提取模板参数以提取其可变模板参数的魔术。
为了提取模板模板参数,我尝试在container_type_holder中添加一个别名,但没有成功,因为它不是一个完整的类型。为了提取可变模板参数,我尝试了相同的策略,但没有成功。
template <template <typename... Args> class Container, typename... Args>
struct container_type_holder
{
using container = Container; // doesnt work
using args = Args...; // ???
};我不知道这是否可能,我是模板世界的乞讨者。
发布于 2018-08-04 14:52:44
您可以使用这些别名来检索模板参数:
template <template <typename... Args> class Container,
typename... Args>
struct container_type_holder
{
template <typename ... Ts>
using container = Container<Ts...>;
constexpr std::size arg_count = sizeof...(Args);
using args_as_tuple = std::tuple<Args...>;
template <std::size_t I>
using get_arg = typename std::tuple_element<I, std::tuple<Args...>::type;
// And possibly helpers, such as
template <template <typename ...> OtherContainer>
using template_rebind = container_type_holder<OtherContainer, Args...>;
};然后,用法可能是:
using c1 = container_type_holder<std::map, std::string, int>;
using c2 = c1::template_rebind<std::unordered_map>;
using c3 = container_type_holder<std::vector, std::pair<c1::get_arg<0>, c1::get_arg<1>>>;发布于 2018-08-04 05:16:32
我不认为你的要求是可能的但是..。您确定不能编写简单地接收类型的container_type_holder吗
template <typename>
struct container_type_holder;并将您的结构开发为部分专门化?
template <template <typename... Args> class Container, typename... Args>
struct container_type_holder<Container<Args...>>
{
// ...
};这种方式是container_type_holder专门化本身,它提取模板-模板参数和可变列表
我的意思是:如果你声明一个对象
container_type_holder<std::map<std::string, int>> obj;在专门化中,Container是std::map,Args...是std::string, int。
https://stackoverflow.com/questions/51680044
复制相似问题