模板:名称解析:依赖类型: - >任何人都可以为此声明说明更多示例吗?

时间:2010-09-27 04:45:26

标签: c++ templates

这是ISO C ++标准14.6.2.1的声明:依赖类型:

A type is dependent if it is
— a template parameter,#1
— a qualified-id with a nested-name-specifier which contains a class-name
   that names a dependent type or whose unqualified-id names a dependent type,#2
— a cv-qualified type where the cv-unqualified type is dependent, #3
— a compound type constructed from any dependent type,#4
— an array type constructed from any dependent type or whose size is specified
    by a constant expression that is value-dependent, #5
— a template-id in which either the template name is a template parameter
   or any of the template arguments is a dependent type or an expression 
   that is type-dependent or value-dependent.#6 

我无法理解最后两点?

任何人都可以举例说明这些陈述(尤其是最后的#5,#6)?

2 个答案:

答案 0 :(得分:4)

1) a type is dependent if it is a template parameter开始工作:

template <typename T, int N, template <typename> class My_Template>
struct X
{

5 - 从任何依赖类型构造的数组类型,或者其大小由与值相关的常量表达式指定,

    T a[5];        // array of dependent type
    int b[N];      // value-dependent size

6 - 模板ID,其中模板名称是模板参数
     或任何模板参数是依赖类型或表达式      这是依赖于类型或依赖于价值的。

    My_Template<int> c;           // template parameter
    Some_Template<T> d;           // template argument is dependent
    Another_Template<sizeof c> e; // type-dependent expression
    Another_Template<N> f;        // value-dependent expression
};

答案 1 :(得分:0)

template<class T> struct Base{ 
}; 

template<class T, int n, template<class X> class U> struct Derived : Base<T>{ 
    T array1[10];       // #5 

    int array2[n];      // #5 

    U<T> u;             // #6 
    Base<T> b;          // #6 
}; 


int main(){}