嵌套可变参数模板类的C ++模糊模板实例化

时间:2020-03-12 23:25:55

标签: c++ templates g++ variadic-templates clang++

请考虑以下内容:

template<class... T>
struct outer
{
  template<int... I>
  struct helper{};

  template<int Num, class = helper<>>
  struct inner;
  template<int Num, int... V>
  struct inner<Num,helper<V...>> : public inner<Num-1,helper<V...,sizeof...(V)>>{};
  template<int... V>
  struct inner<0, helper<V...>>{
    using type = helper<V...>;
  };
};

int main(){
  outer<int>::inner<7>::type i;
}

我发现这段代码将使用clang-9.0.1进行编译,而不会出现错误,但是使用gcc-9.2.0则会失败,并出现以下错误:

test2.cpp: In instantiation of ‘struct outer<int>::inner<1, outer<int>::helper<0, 1, 2, 3, 4, 5> >’:
test2.cpp:10:9:   recursively required from ‘struct outer<int>::inner<6, outer<int>::helper<0> >’
test2.cpp:10:9:   required from ‘struct outer<int>::inner<7>’
test2.cpp:18:22:   required from here
test2.cpp:10:9: error: ambiguous template instantiation for ‘struct outer<int>::inner<0, outer<int>::helper<0, 1, 2, 3, 4, 5, 6> >’
   10 |  struct inner<Num,helper<V...>> : public inner<Num-1,helper<V...,sizeof...(V)>>{};
      |         ^~~~~~~~~~~~~~~~~~~~~~~
test2.cpp:10:9: note: candidates are: ‘template<class ... T> template<int Num, int ...V> struct outer<T>::inner<Num, outer<T>::helper<V ...> > [with int Num = 0; int ...V = {0, 1, 2, 3, 4, 5, 6}; T = {int}]’
test2.cpp:12:9: note:                 ‘template<class ... T> template<int ...V> struct outer<T>::inner<0, outer<T>::helper<V ...> > [with int ...V = {0, 1, 2, 3, 4, 5, 6}; T = {int}]’
   12 |  struct inner<0, helper<V...>>{
      |         ^~~~~~~~~~~~~~~~~~~~~~
test2.cpp:10:9: error: invalid use of incomplete type ‘struct outer<int>::inner<0, outer<int>::helper<0, 1, 2, 3, 4, 5, 6> >’
   10 |  struct inner<Num,helper<V...>> : public inner<Num-1,helper<V...,sizeof...(V)>>{};
      |         ^~~~~~~~~~~~~~~~~~~~~~~
test2.cpp:8:9: note: declaration of ‘struct outer<int>::inner<0, outer<int>::helper<0, 1, 2, 3, 4, 5, 6> >’
    8 |  struct inner;
      |         ^~~~~
test2.cpp: In function ‘int main()’:
test2.cpp:18:24: error: ‘type’ is not a member of ‘outer<int>::inner<7>’
   18 |  outer<int>::inner<7>::type i;
      |   

我已经看到类似的递归继承在gcc中成功了,但是当它在可变参数模板类内部时似乎并不成功。还要注意的是,将外部模板更改为template<class T>可以修复gcc错误。

这是clang还是gcc错误?

0 个答案:

没有答案