gpt4 book ai didi

c++ - 嵌套两次的 sizeof 可以是依赖表达式吗?

转载 作者:IT老高 更新时间:2023-10-28 22:22:02 30 4
gpt4 key购买 nike

我注意到 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;
};

这两个编译器似乎在 sizeof(sizeof(T))是依赖于类型或依赖于值的表达式。如果表达式是相关的,那么 I<sizeof(sizeof(T))>是依赖类型,这意味着 typename应该是必需的。

C++11 标准中的以下措辞涵盖了这一点:

[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

[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

Expressions of the following form are value-dependent if the unary-expression or expression is typedependent or the type-id is dependent:

sizeof unary-expression
sizeof ( type-id )

我的解释是 sizeof(T)永远不能依赖于类型,这意味着 sizeof(sizeof(T))永远不能依赖于类型或依赖于值。

这是 gcc 中的错误吗?

最佳答案

我正在使用 N4296 后的草稿。

typedef I<sizeof(sizeof(T))>::Type Type;

typename如果 nested-name-specifier I<..> 是必需的取决于模板参数 [temp.res]/5。所以,是I<..>依赖?

[temp.dep.type]/9 A type is dependent if it is

  • [...]
  • (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 [...]

I<..>是一个simple-template-id,模板参数是一个表达式。这是表达式sizeof(sizeof(T))类型相关还是值相关?

表达式 sizeof(sizeof(T))可以分解为以下表达式:

expression           form===============================================              T      type-id       sizeof(T)     sizeof ( type-id )      (sizeof(T))    ( expression )sizeof(sizeof(T))    sizeof unary-expression

T不是一个表达式,但我会把它留在列表中以备后用。关于括号的注释:primary-expression 可以是带括号的(通用)表达式unary-expression 可以是 postfix-expression 也可以是 primary-expression,因此它也可以用括号括起来。

带括号的表达式 (X)取决于 X是依赖的:

[temp.dep.expr]/1 Except as described below, an expression is type-dependent if any subexpression is type-dependent.

[temp.dep.constexpr]/1 Except as described below, a constant expression is value-dependent if any subexpression is value-dependent.

一般来说,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):

[...]
sizeof unary-expression
sizeof ( type-id )

但是,它们产生的值可能取决于模板参数:

[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:

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          nosizeof(sizeof(T))    sizeof unary-expression    no           no

自从 T依赖于类型sizeof(T)变得依赖于值(value)。但是,由于 (sizeof(T)) 不依赖于类型sizeof(sizeof(T))完全不依赖。

关于c++ - 嵌套两次的 sizeof 可以是依赖表达式吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27713222/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com