【问题标题】:Error when passing template template parameter with dependent nested parameters types传递具有依赖嵌套参数类型的模板模板参数时出错
【发布时间】:2017-02-15 13:36:37
【问题描述】:

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

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>> { ... };

这些技巧是标准禁止的还是仅仅是编译器的限制?

【问题讨论】:

  • 不会在 gcc 或 clang 上编译,fwiw。 clang 和 msvc 一样的错误,gcc 的不同,意义不大。
  • @Barry 在 clang 3.9.1 上运行,但在 clang 5.0 SVN 上没有
  • @TemplateRex 适合我思考更新 == 最佳 :)
  • @Barry 奇怪的是,这对所有直到 SVN ToT 的 clang 版本都有效(将 c++17 之前的 typename 更改为 class),但对于 g++ 来说从来没有/跨度>

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


【解决方案1】:

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

我可以产生这个解决方法。

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.

【讨论】:

    猜你喜欢
    • 2010-12-17
    • 1970-01-01
    • 2014-06-07
    • 1970-01-01
    • 1970-01-01
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    相关资源
    最近更新 更多