直接初始化无符号short的标准行为 [英] Standard behavior for direct initialization of unsigned short

查看:551
本文介绍了直接初始化无符号short的标准行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天注意到在示例代码中:

I noticed today that in the example code:

void print(unsigned short a) {
   std::cout << a << std::endl;
}

初始化和使用的方式如下:

Initialization and use works like this:

print(short (5));

但不是这样:

print(unsigned short(6));

main.cpp:16:8:错误:未签名"之前的预期主表达式 print(unsigned short(6));

main.cpp:16:8: error: expected primary-expression before 'unsigned' print(unsigned short(6));

与类型无关,因为它也可以工作:

And it's not to do with the type since this also works:

typedef unsigned short ushort;
print(ushort (6));

在线示例.

因此,我去搜索标准关于值初始化的内容.没结果:

So I went searching for what the standard says about value initialization. It turns out nothing:

值初始化的影响是:

The effects of value initialization are:

1)如果T是一个类类型...

1) if T is a class type ...

2)如果T是非联合类类型...

2) if T is a non-union class type ...

2)如果T是一个类类型...

2) if T is a class type ...

3)如果T是数组类型,..

3) if T is an array type, ..

4)否则,该对象将被初始化为零.

4) otherwise, the object is zero-initialized.

为便于阅读而进行的修改. 原始来源.

关于 POD 类型的值初始化的规则是什么?无法对unsigned限定类型进行值初始化的原因是什么?它们是否与rvalues有关?

What are the rules about value initialization of POD types? What is the reason that unsigned qualified types can't be value initialized? Is this more to do with the fact they are rvalues?

推荐答案

不能对unsigned限定类型进行值初始化的原因是什么?

What is the reason that unsigned qualified types can't be value initialized?

这仅仅是因为在功能转换表达式,而unsigned short不是单字类型名称; short是.

It's just because only single-word type name could be used in functional cast expression, while unsigned short is not a single-word type name; short is.

函数强制转换表达式由一个简单的类型说明符或一个typedef说明符组成(换句话说,单个单词类型名称:unsigned int(expression)int*(expression)无效),后跟一个括号中的单个表达式.

The functional cast expression consists of a simple type specifier or a typedef specifier (in other words, a single-word type name: unsigned int(expression) or int*(expression) are not valid), followed by a single expression in parentheses.

如您所显示的,您可以使用typedef作为解决方法,或者添加括号以将其更改为c样式的强制转换表达式,例如(unsigned short)(6)(unsigned short)6.

As you showed, you can use typedef as the workaround, or add parentheses to change it to c-style cast expression, e.g. (unsigned short)(6), or (unsigned short)6.

根据标准§7.6.1.3/1显式类型转换(功能符号)[expr.type.conv] :

简单类型说明符

A simple-type-specifier or typename-specifier followed by a parenthesized optional expression-list or by a braced-init-list (the initializer) constructs a value of the specified type given the initializer.

简单类型说明符:

simple-type-specifier:
  nested-name-specifier opt
 type-name
  nested-name-specifier template simple-template-id
  nested-name-specifier opt
 template-name
  char
  char16_t
  char32_t
  wchar_t
  bool
  short
  int
  long
  signed
  unsigned
  float
  double
  void
  auto
  decltype-specifier
type-name:
  class-name
  enum-name
  typedef-name
  simple-template-id
decltype-specifier:
  decltype ( expression )
  decltype ( auto )

typename-specifier :

typename-specifier:
  typename nested-name-specifier identifier
  typename nested-name-specifier template opt
 simple-template-id

这篇关于直接初始化无符号short的标准行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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