[英]Can sizeof nested twice ever be a dependent expression?
I noticed that gcc 5.0 rejects the following code, while clang 3.6 accepts it. 我注意到gcc 5.0拒绝以下代码,而clang 3.6接受它。
template<int n>
struct I
{
typedef int Type;
};
template<typename T>
struct A
{
typedef I<sizeof(sizeof(T))>::Type Type;
};
The two compilers seem to differ on whether sizeof(sizeof(T))
is a type-dependent or value-dependent expression. 这两个编译器似乎在
sizeof(sizeof(T))
是依赖于类型还是依赖于值的表达式上有所不同。 If the expression were dependent, then it follows that I<sizeof(sizeof(T))>
is a dependent type, meaning that typename
should be required. 如果表达式是依赖的,那么
I<sizeof(sizeof(T))>
是一个依赖类型,这意味着typename
应该是必需的。
This is covered by the following wording in the C++11 standard: 这由C ++ 11标准中的以下措辞涵盖:
[temp.dep.type]/8
[temp.dep.type] / 8
A type is dependent if it is
如果是,则类型依赖于
- a simple-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
一个simple-template-id,其中模板名称是模板参数或任何模板参数是依赖类型或依赖于类型或依赖于值的表达式
[temp.dep.expr]/4
[temp.dep.expr / 4
Expressions of the following forms are never type-dependent (because the type of the expression cannot be dependent):
以下表单的表达式从不依赖于类型(因为表达式的类型不能依赖):
sizeof unary-expression sizeof ( type-id )
[temp.dep.constexpr]/2
[temp.dep.constexpr] / 2
Expressions of the following form are value-dependent if the unary-expression or expression is typedependent or the type-id is dependent:
如果unary-expression或expression是typedependent或type-id是依赖的,则以下形式的表达式是值依赖的:
sizeof unary-expression sizeof ( type-id )
My interpretation is that sizeof(T)
can never be type-dependent, meaning sizeof(sizeof(T))
can never be type-dependent or value-dependent. 我的解释是
sizeof(T)
永远不会依赖于类型,这意味着sizeof(sizeof(T))
永远不能依赖于类型或依赖于值。
Is this a bug in gcc? 这是gcc中的错误吗?
I'm using a post-N4296 draft. 我正在使用N4296之后的草稿。
typedef I<sizeof(sizeof(T))>::Type Type;
typename
is required if the nested-name-specifier I<..>
depends on a template parameter [temp.res]/5. 如果嵌套名称说明符
I<..>
依赖于模板参数[temp.res] / 5,则需要typename
。 So, is I<..>
dependent? 那么,
I<..>
是依赖吗?
[temp.dep.type]/9 A type is dependent if it is
[temp.dep.type] / 9如果是,则类型是依赖的
- [...]
[...]
- (9.7) a simple-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 , or [...]
(9.7) simple-template-id ,其中模板名称是模板参数或任何模板参数是依赖类型或依赖于类型或依赖于 值的表达式,或者[...]
I<..>
is a simple-template-id , the template argument is an expression. I<..>
是一个simple-template-id ,模板参数是一个表达式。 Is this expression sizeof(sizeof(T))
type-dependent or value-dependent? 这个表达式
sizeof(sizeof(T))
是依赖于类型还是取决于值?
The expression sizeof(sizeof(T))
can be broken down into the following expressions: 表达式
sizeof(sizeof(T))
可以分解为以下表达式:
expression form =============================================== T type-id sizeof(T) sizeof ( type-id ) (sizeof(T)) ( expression ) sizeof(sizeof(T)) sizeof unary-expression
T
is not an expression, but I'll leave it in the list for later. T
不是表达式,但我会将它留在列表中供以后使用。 A note on the parentheses: A primary-expression can be a parenthesized (general) expression . 关于括号的注释: primary-expression可以是带括号的(通用) 表达式 。 A unary-expression can be a postfix-expression which can be a primary-expression , hence it can be parenthesized, too.
一元表达式可以是后缀表达式 ,它可以是一个主表达式 ,因此也可以用括号括起来。
A parenthesized expression (X)
is dependent if X
is dependent: 如果
X
是依赖的,带括号的表达式(X)
是依赖的:
[temp.dep.expr]/1 Except as described below, an expression is type-dependent if any subexpression is type-dependent.
[temp.dep.expr] / 1除了如下所述,如果任何子表达式依赖于类型,则表达式依赖于类型。
[temp.dep.constexpr]/1 Except as described below, a constant expression is value-dependent if any subexpression is value-dependent.
[temp.dep.constexpr] / 1除了如下所述,如果任何子表达式依赖于值,则常量表达式依赖于值。
In general, sizeof
expressions are never type -dependent, because they always produce a value of type std::size_t
: 通常,
sizeof
表达式从不依赖于类型 ,因为它们总是生成std::size_t
类型的值:
[temp.dep.expr]/4 Expressions of the following forms are never type-dependent (because the type of the expression cannot be dependent):
[temp.dep.expr] / 4以下表单的表达式从不依赖于类型(因为表达式的类型不能依赖):
[...] sizeof unary-expression sizeof ( type-id )
However, the value they yield can be dependent on a template parameter: 但是,它们产生的值可能取决于模板参数:
[temp.dep.constexpr]/2 Expressions of the following form are value-dependent if the unary-expression or expression is type-dependent or the type-id is dependent :
[temp.dep.constexpr] / 2 如果unary-expression或expression依赖于类型或type-id依赖 ,则以下形式的表达式是值依赖的 :
sizeof unary-expression sizeof ( type-id )
expression form value-dep? type-dep? ======================================================================= T type-id no yes sizeof(T) sizeof ( type-id ) yes no (sizeof(T)) ( expression ) yes no sizeof(sizeof(T)) sizeof unary-expression no no
Since T
is type -dependent, sizeof(T)
becomes value -dependent. 由于
T
是类型依赖的,因此sizeof(T)
变为值依赖的。 However, since (sizeof(T))
is not type -dependent, sizeof(sizeof(T))
is not dependent at all. 但是,由于
(sizeof(T))
不依赖于类型 ,因此sizeof(sizeof(T))
完全不依赖。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.