c++ - 扩展模板<class>类的参数包

标签 c++ templates c++11 variadic-templates template-templates

假设我有一些模板类定义如下

template<template<class>class...>
struct my_class;

template<class>
struct define_template{
    template<class>
    class type;
};

我需要定义一个别名模板,将 define_template::type 替换为 my_class 所以对于三个类(class)我可以做到这一点

template<class A, class B, class C>
using my_alias = my_class<  define_template<A>::template type, 
                            define_template<B>::template type, 
                            define_template<C>::template type>;

对于可变参数模板,我无法计算出执行此操作的语法,理想情况下是这样的

template<class... T>
using new_class = my_class<define_template<T>::template type...>;

这给了我一个错误“参数包没有用'...'扩展”

有人知道正确的语法吗?

从下面的评论来看,它在 clang 中编译,我通过 Cygwin 使用 gcc 4.8.2。

最佳答案

我假设这是 gcc 中的一个错误,所以我现在已经做了一个解决方法

// partially specializes a template
template<template<class, class>class TT, class T>
struct TemplateBind{
    template<class S>
    using type = TT<T, S>;
};


// the workaround
template<template<template<class>class...>class A,
         template<class, class>class B,
         template<class>class... C>
class workaround {
    template<class D, class... E>
    static auto get_type(D, E...)
    -> typename workaround<A, B, C..., TemplateBind<B, D>::template type>::template type<E...>;

    static auto get_type()
    ->A<C...>;

public:
    template<class... T>
    using type = decltype(get_type(std::declval<T>()...));

};

// convinience alias
template<template<template<class>class...>class OriginalTemplate,
         template<class, class>class Substitution,
         class... Types>
using InstatiateVariadicTemplateTemplate = typename workaround<OriginalTemplate, Substitution>::template type<Types...>;

然后我们可以为define_template定义一个包装器

// wrapper alias gets  define_template in the right form
template<class A, class B>
using my_wrapper = typename define_template<A>::template type<B>;

并实例化如下

template<class... T>
using my_alias = InstatiateVariadicTemplateTemplate<my_class, my_wrapper, T...>;

关于c++ - 扩展模板<class>类的参数包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26508349/

相关文章:

c++ - 将指针或引用传递给派生类,以便调用基类方法

c++ - 对于 64 位 Windows,是否有 libusb-win32 的替代方案?

python - 如何统计模板匹配检测到的对象数量?

c++ - 为什么我会收到 Unresolved external 问题?

c++ - 如何制作通用精度 ISO 时间戳生成器

c++ - Qt QSerialPort 缓冲

c++ - D3D11 : variable number of lights in HLSL

C++ 部分类模板针对多个参数的特化

c++ - 使用 std::find 这种方式的正确性

c++ - 调用 std::map::emplace() 并避免不必要的构造