Why does gcc complain “error: type 'intT' of template argument '0' depends on a template parameter”?

孤人 提交于 2019-12-12 15:54:10

问题


My compiler is gcc 4.9.0. The following code cannot be compiled:

template<typename T, T i>
struct value {};

template<typename T>
struct value<T, 0> {};
// error: type 'T' of template argument '0' depends on a template parameter

What is the cause? and, how to solve this issue?


回答1:


GCC is right, this is explicitly forbidden by C++11 [temp.class.spec] §8:

8 Within the argument list of a class template partial specialization, the following restrictions apply:

  • A partially specialized non-type argument expression shall not involve a template parameter of the partial specialization except when the argument expression is a simple identifier. [ Example:

    template <int I, int J> struct A {};
    template <int I> struct A<I+5, I*2> {}; // error
    template <int I, int J> struct B {};
    template <int I> struct B<I, I> {}; // OK
    

    —end example ]

  • The type of a template parameter corresponding to a specialized non-type argument shall not be dependent on a parameter of the specialization. [ Example:

    template <class T, T t> struct C {};
    template <class T> struct C<T, 1>; // error
    template< int X, int (*array_ptr)[X] > class A {};
    int array[5];
    template< int X > class A<X,&array> { }; // error
    

    —end example ]

  • ...

I believe point 2 is the most relevant one here.


Regarding the question of "how to solve this issue." In the form the question stands now, there is no workaround, I am afraid.

As for the original vesion with making integer sequences, I believe that you could make it work with using uintmax_t for the type of the non-type template parameter, and only convert it to intT in the final definition.



来源:https://stackoverflow.com/questions/24656212/why-does-gcc-complain-error-type-intt-of-template-argument-0-depends-on-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!