什么是decltype(0 + 0)? [英] What is decltype(0 + 0)?

查看:114
本文介绍了什么是decltype(0 + 0)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(由提供答案



给定N3290,§7.1.6.2p4,其中列表项未编号,但为了方便起见编号:


如果e是未加括号的id表达式或未加括号的类成员访问(5.2),则由decltype(e)表示的类型定义如下:


    < .5),decltype(e)是由e命名的实体的类型。如果没有这样的实体,或者如果e命名一组重载的函数,则程序是不成形的;
  1. 否则,如果e是x值,则decltype(e)是T& &其中T是e的类型;

  2. 否则,如果e是左值,则decltype(e)是T&,其中T是e的类型;
  3. 否则,decltype(e)是e的类型。




<什么是decltype(0 + 0)指定的类型?



项目1不适用,2可能,但如果不是,则3不适用,将是结果。那么,什么是xvalue,是0 + 0是xvalue?



§3.10p1:


xvalue(eXpiring值)也指一个对象,通常接近其生命周期的终点(例如,它的资源可能被移动)。 xvalue是涉及右值引用的某些种类的表达式的结果(8.3.2)。


8.3.2这将是有帮助的,但我知道0 + 0不涉及任何右值引用。文字0是一个prvalue,它是不是xvalue的右值(§3.10p1)。我相信0 + 0也是贬值。如果是这样,decltype(0 + 0)将是int(不是&&)。



我在我的解释中错过了什么吗?这个代码是否格式良好?

  decltype(0 + 0)x; //未初始化。 

代码在GCC 4.7.0 20110427和Clang 2.9(trunk 126116)上编译。如果decltype指定了一个int&&& c> ,每个常量表达式是一个prvalue。


一个prvalue核心常量表达式的文字类型,但不是指针类型。


因此,规则4适用于以下情况: 到所有字面常量表达式。


(Prompted by an answer.)

Given N3290, §7.1.6.2p4, where the list items are unnumbered, but numbered here for our convenience:

The type denoted by decltype(e) is defined as follows:

  1. if e is an unparenthesized id-expression or an unparenthesized class member access (5.2.5), decltype(e) is the type of the entity named by e. If there is no such entity, or if e names a set of overloaded functions, the program is ill-formed;
  2. otherwise, if e is an xvalue, decltype(e) is T&&, where T is the type of e;
  3. otherwise, if e is an lvalue, decltype(e) is T&, where T is the type of e;
  4. otherwise, decltype(e) is the type of e.

What is the type specified by decltype(0 + 0)?

Item 1 doesn't apply, 2 might, but if not, then 3 doesn't apply and 4 would be the result. So, what is an xvalue, and is 0 + 0 an xvalue?

§3.10p1:

An xvalue (an "eXpiring" value) also refers to an object, usually near the end of its lifetime (so that its resources may be moved, for example). An xvalue is the result of certain kinds of expressions involving rvalue references (8.3.2).

I don't see anything in §8.3.2 that would be helpful here, but I do know "0 + 0" doesn't involve any rvalue-references. The literal 0 is a prvalue, which is "an rvalue that is not an xvalue" (§3.10p1). I believe "0 + 0" is also a prvalue. If that's true, "decltype(0 + 0)" would be int (not int&&).

Have I missed something in my interpretation? Is this code well-formed?

decltype(0 + 0) x;  // Not initialized.

The code compiles on GCC 4.7.0 20110427 and Clang 2.9 (trunk 126116). It would not be well-formed if the decltype specified an int&& type, for example.

解决方案

From 5.19 [expr.const], every literal constant expression is a prvalue.

A literal constant expression is a prvalue core constant expression of literal type, but not pointer type. An integral constant expression is a literal constant expression of integral or unscoped enumeration type.

Therefore rule 4 applies to all literal constant expressions.

这篇关于什么是decltype(0 + 0)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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