gcc为什么会抱怨“错误:模板参数'0'的类型'intT'取决于模板参数”? [英] Why does gcc complain "error: type 'intT' of template argument '0' depends on a template parameter"?

查看:95
本文介绍了gcc为什么会抱怨“错误:模板参数'0'的类型'intT'取决于模板参数”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的编译器是gcc 4.9.0。无法编译以下代码:

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

是什么原因?

推荐答案

GCC是正确的,这在C ++ 11中被明确禁止[temp.class .spec]§8:

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


8在类模板部分专业化的参数列表中,适用以下限制:

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


  • 部分专用的非类型参数表达式不应包含
    部分专用的模板参数,除非参数表达式是简单的标识符。 [示例:

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

-结束示例]

与专门的非类型参数相对应的模板参数的类型不得为
,这取决于专门化的参数。 [示例:

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

-结束示例]

...

I相信第2点是最相关的点。

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.

至于具有整数序列的原始版本,我相信您可以使用 uintmax_t 作为非类型模板参数的类型,并将其仅转换为 intT

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.

这篇关于gcc为什么会抱怨“错误:模板参数'0'的类型'intT'取决于模板参数”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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