使用外部类的可变参数模板中的参数部分特化可变参数模板内部类是否合法

2024-01-23

考虑代码:

#include <iostream>

template <class... Ts>
struct outer {
   template <class... ITs>
   struct inner {
      static constexpr bool value = false;
   };

   template <class... ITs>
   struct inner<Ts..., ITs...> {   
      static constexpr bool value = true;
   };
};

int main() {
   std::cout << outer<int, float, double>::inner<int, float, double, int>::value << std::endl;
}

该代码使用 clang++ 编译,但不使用 g++ 编译,会产生错误:

temp3.cc:11:11: 错误:参数包参数“Ts ...”必须位于 模板参数列表的末尾

struct inner<Ts..., ITs...> {
       ^

正如我已经确定的here https://stackoverflow.com/questions/37766902/is-it-legit-to-specialize-variadic-template-class-inside-other-template-class内部类的部分专业化应该是合法的。

Edit:为了完整起见,值得补充的是,上述代码的 clang 警告他可能在推导 IT 参数时遇到问题,但执行起来没有任何问题......


这是一个海湾合作委员会错误。这是一个完全有效的部分特化:

template <class... ITs>
struct inner<Ts..., ITs...> {   
   static constexpr bool value = true;
};

推导的模板参数包必须是最后一个,并且ITs...满足这一点。但Ts...这里并不是需要推导的包,只是一个具体的参数包。

此外,gcc 编译了几个等效的公式:

template <class... Ts>
struct X {
    template <class... Us>
    static void foo(Ts..., Us...) { }
};

int main() {
   X<int>::foo(1, 'c');
}

and:

template <class... Us>
struct A { };

template <class... Ts>
struct X {
    template <class... Us>
    static void foo(A<Ts..., Us...>) { }
};

int main() {
   X<int>::foo(A<int, char>{});
}

这些与您的原始示例等效。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用外部类的可变参数模板中的参数部分特化可变参数模板内部类是否合法 的相关文章

随机推荐