嵌套两次的sizeof可以成为从属表达式吗? [英] Can sizeof nested twice ever be a dependent expression?

查看:98
本文介绍了嵌套两次的sizeof可以成为从属表达式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到gcc 5.0拒绝了以下代码,而clang 3.6接受了它.

I noticed that gcc 5.0 rejects the following code, while clang 3.6 accepts it.

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.

The two compilers seem to differ on whether sizeof(sizeof(T)) is a type-dependent or value-dependent expression. If the expression were dependent, then it follows that I<sizeof(sizeof(T))> is a dependent type, meaning that typename should be required.

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

This is covered by the following wording in the C++11 standard:

[临时类型]/8

[temp.dep.type]/8

类型是否依赖于

  • 一个简单模板ID,其中模板名称是模板参数或任何模板 arguments是从属类型或与类型有关或与值有关的表达式
  • 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

[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

如果一元表达式或表达式是类型相关的,则以下形式的表达式是值相关的 或type-id是依赖的:

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))永远不会依赖类型或值.

My interpretation is that sizeof(T) can never be type-dependent, meaning sizeof(sizeof(T)) can never be type-dependent or value-dependent.

这是gcc中的错误吗?

Is this a bug in gcc?

推荐答案

我正在使用N4296之后的草案.

I'm using a post-N4296 draft.

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

如果嵌套名称说明符 I<..>取决于模板参数[temp.res]/5,则需要

typename.那么,I<..>是否依赖?

typename is required if the nested-name-specifier I<..> depends on a template parameter [temp.res]/5. So, is I<..> dependent?

[temp.dep.type]/9类型是否依赖

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

  • [...]
  • (9.7)一个 simple-template-id ,其中模板名称是模板参数,或者任何模板参数是从属 类型或取决于类型取决于值的表达式,或者 [...]
  • [...]
  • (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 ,template参数是一个表达式.此表达式是sizeof(sizeof(T))类型依赖还是值依赖?

I<..> is a simple-template-id, the template argument is an expression. 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:


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

T不是表达式,但我将其留在列表中以备后用.括号中的注释: primary-expression 可以是带括号的(通用) expression .一个 unary-expression 可以是一个 postfix-expression ,它可以是一个 primary-expression ,因此也可以加上括号.

T is not an expression, but I'll leave it in the list for later. A note on the parentheses: A primary-expression can be a parenthesized (general) expression. A unary-expression can be a postfix-expression which can be a primary-expression, hence it can be parenthesized, too.

如果X是从属的,带括号的表达式(X)是从属的:

A parenthesized expression (X) is dependent if X is dependent:

[temp.dep.expr]/1除如下所述,如果子表达式与类型有关,则表达式与类型有关.

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

[temp.dep.constexpr]/1除如下所述,如果任何子表达式与值相关,则常量表达式与值相关.

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

通常,sizeof表达式从不依赖 type ,因为它们始终产生std::size_t类型的值:

In general, sizeof expressions are never type-dependent, because they always produce a value of type std::size_t:

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

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

However, the value they yield can be dependent on a template parameter:

[temp.dep.constexpr]/2如果 unary-expression expression 与类型相关或 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          no
sizeof(sizeof(T))    sizeof unary-expression    no           no

由于T类型依赖项,因此sizeof(T)成为 value 依赖项.但是,由于(sizeof(T))不依赖类型的,因此sizeof(sizeof(T))完全不依赖.

Since T is type-dependent, sizeof(T) becomes value-dependent. However, since (sizeof(T)) is not type-dependent, sizeof(sizeof(T)) is not dependent at all.

这篇关于嵌套两次的sizeof可以成为从属表达式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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