部分专门化可变参数模板内部类与外部类的可变参数模板的args是合法的 [英] Is it legal to partially specialise variadic template inner class with args from variadic template of an outer class

查看:122
本文介绍了部分专门化可变参数模板内部类与外部类的可变参数模板的args是合法的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑代码:

#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 ++进行编译, / p>

The code compiles with clang++ but not with g++ where it produces an error:


temp3.cc:11:11:error:参数包参数'Ts ...'必须位于
模板参数列表

temp3.cc:11:11: error: parameter pack argument ‘Ts ...’ must be at the end of the template argument list

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


a href =http://stackoverflow.com/questions/37766902/is-it-legit-to-specialize-variadic-template-class-inside-other-template-class>这里部分专业化内部类应该是合法的。

As I've already established here partial specialisation of the inner class should be legit.

编辑:
为了完整性,值得添加上述代码的俚语警告他可能有一个问题,参数,但没有任何问题...

For completeness it is worth adding that clang for the above code warns that he might have a problem with deducing ITs parameters yet doing it without any problems...

推荐答案

这是一个gcc错误。这是一个完全有效的部分专业化:

This is a gcc bug. This is a perfectly valid partial specialization:

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

推导出的模板参数包必须是最后一个, ITs ... 满足。但 Ts ... 不是一个需要在这里推导出来的包,它只是一个特定的参数包。

Deduced template parameter packs must be last, and ITs... satisfies that. But Ts... isn't a pack that needs to be deduced here, it's just a specific parameter pack.

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

Furthermore, gcc compiles several equivalent formulations:

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

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

和:

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

这些与原始示例的格式相同。

These are equivalently well-formed to your original example.

这篇关于部分专门化可变参数模板内部类与外部类的可变参数模板的args是合法的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆