显式类型转换和多个简单类型说明符 [英] Explicit Type Conversion and Multiple Simple Type Specifiers

查看:139
本文介绍了显式类型转换和多个简单类型说明符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要初始化类型 T 的对象的值,将按照以下之一的行处理:

  T x = T(); 
T x((T()));我的问题是关于通过简单类型说明符组合指定的类型,例如 unsigned int

  unsigned int x = unsigned int(); 
unsigned int x((unsigned int())); Visual C ++ 2008和英特尔®C ++编译器11.1接受这两个没有警告;



Comeau 4.3.10.1b2和g ++ 3.4.5(这是,不可否认,不是特别新近)不。



根据C ++标准(C ++ 03 5.2。 3/2,expr.type.conv):


表达式 T(),其中 T 是非数组完整对象类型的简单类型说明符(7.1.5.2)或(可能是cv限定的) void type,创建一个指定类型的值,它是值初始化的


7.1.5.2说,简单类型说明符是,然后是包含 unsigned int 的列表。



因此,假设在5.2.3 / 2中,simple-type-specifier是单数, unsigned int 是两个类型说明符,是上面使用 unsigned int 无效的示例吗? (如果是这样,后续的是,微软和英特尔是否不支持所述表达式?)



这个问题更多的是出于好奇心,对于由多个简单类型说明符组合指定的所有类型,值初始化等效于零初始化。 (此问题是由对此回答的评论提示的)对于初始化问题)。

解决方案

将此问题发布到comp.lang.c ++。moderated



C ++标准委员会的DanielKrügler同意以下解释: unsigned int 是简单类型说明符的组合,本身不是简单的类型说明符



关于表7的说明由Jerry Coffin引用,Krügler说:


我同意表7(最近
最近草案N3000中的表9)有些误导,但是在[dcl.type.simple] / 2中前面的
文本对我来说很清楚,当它说:



表7总结了简单类型说明符
及其指定的类型的有效组合。



To value initialize an object of type T, one would do something along the lines of one of the following:

T x = T();
T x((T()));

My question concerns types specified by a combination of simple type specifiers, e.g., unsigned int:

unsigned int x = unsigned int();
unsigned int x((unsigned int()));

Visual C++ 2008 and Intel C++ Compiler 11.1 accept both of these without warnings; Comeau 4.3.10.1b2 and g++ 3.4.5 (which is, admittedly, not particularly recent) do not.

According to the C++ standard (C++03 5.2.3/2, expr.type.conv):

The expression T(), where T is a simple-type-specifier (7.1.5.2) for a non-array complete object type or the (possibly cv-qualified) void type, creates an rvalue of the specified type, which is value-initialized

7.1.5.2 says, "the simple type specifiers are," and follows with a list that includes unsigned and int.

Therefore, given that in 5.2.3/2, "simple-type-specifier" is singular, and unsigned and int are two type specifiers, are the examples above that use unsigned int invalid? (and, if so, the followup is, is it incorrect for Microsoft and Intel to support said expressions?)

This question is more out of curiosity than anything else; for all of the types specified by a combination of multiple simple type specifiers, value initialization is equivalent to zero initialization. (This question was prompted by comments in response to this answer to a question about initialization).

解决方案

I posted this question to comp.lang.c++.moderated.

Daniel Krügler of the C++ standards committee agreed with the interpretation that unsigned int is a combination of simple type specifiers, and is not itself a simple type specifier.

Concerning the caption of table 7 referenced by Jerry Coffin, Krügler says:

I agree that the header of Table 7 (which is Table 9 in the most recent draft N3000) is somewhat misleading, but the preceeding text in [dcl.type.simple]/2 looks very clear to me, when it says:

Table 7 summarizes the valid combinations of simple-type-specifiers and the types they specify."

(I apologize it took me so long to post this back here from the newsgroup; it completely slipped my mind)

这篇关于显式类型转换和多个简单类型说明符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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