c++ - 传递具有依赖嵌套参数类型的模板模板参数时出错

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

为什么这个结构不起作用?

Visual Studio 显示错误 C3201:类模板“AA”的模板参数列表与模板参数“C”的模板参数列表不匹配。但是好像是<int, char, bool>在这两种情况下。

template<int I, char C, bool B>
struct AA
{
    static const int  i = I;
    static const char c = C;
    static const bool b = B;
};

template<typename... T>
struct outer
{
    template <template<T... > typename C>
    struct inner
    {
        template<T... X>
        using type = C<X...>;
    };
};

static_assert(outer<int, char, bool>::inner<AA>::type<5, 'a', true>::i == 5, "???");

添加:此外,编译器无法推断像这样的特化类型
template<class T, template<T> class C, T X>
struct A<C<X>> { ... };

标准是否禁止此类技巧,还是仅仅是编译器限制?

最佳答案

我怀疑这是允许的,这只是编译器搞砸了。当我尝试使用它来获得解决方法时,我遇到了很多内部编译器错误;这通常表明它没有被故意拒绝,而且错误消息毫无意义。
我可以生成此解决方法。

template<int I, char C, bool B>
struct AA
{
    static const int  i = I;
    static const char c = C;
    static const bool b = B;
};

template<template<auto... > typename C, typename... Ts>
struct outer_base
{
    struct inner
    {
        template<Ts... X>
        using type = C<X...>;
    };
};


template<typename... Ts>
struct outer
{
    template <template<auto... > typename C>
    using inner = typename outer_base<C, Ts...>::inner;
};

static_assert(outer<int, char, bool>::inner<AA>::type<5, 'a', true>::i == 5, "???");
这比您想要的要少一些,因为它不需要 C匹配类型 Ts...确切地说,只是与它们兼容。
Live example .

关于c++ - 传递具有依赖嵌套参数类型的模板模板参数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42250921/

相关文章:

c++ - C++中带有空尖括号的模板<>是什么意思?

c++ - 带主题的 C/C++ 编译器和编辑器

c++ - 参数包的递归迭代

C++函数返回数组

c++ - 如何使用 QPainter 或 QPainterPath 使用一个形状或一组连接的形状在 Qt 中绘制自定义形状

C++ 模板成员函数已定义为相同类型

c++ - 有没有办法提取模板的参数列表以放入另一个元函数?

c++ - C++ 98 标准在哪里指定本地声明的模板名称不依赖?

c++ - 从模板化模板类和可变模板中声明 "container"对象

c++ - 从另一个模板引用可变模板参数包