繁体   English   中英

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

[英]Error when passing template template parameter with dependent nested parameters types

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

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...确切地说,只需与它们兼容即可。

活生生的例子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM