新汽车会做什么? [英] What does `new auto` do?
问题描述
使用new auto
是什么意思?考虑以下表达式:
What does it mean when I use new auto
? Consider the expression:
new auto(5)
动态分配的对象是什么类型?它返回的指针是什么类型?
What is the type of the dynamically allocated object? What is the type of the pointer it returns?
推荐答案
在这种情况下,auto(5)
解析为int(5)
.
In this context, auto(5)
resolves to int(5)
.
您正在从堆中分配一个新的int
,并将其初始化为5
.
You are allocating a new int
from the heap, initialized to 5
.
(因此,它返回一个int *
)
在得到许可的情况下引用安迪·普罗的足智多谋的答案:
Quoting Andy Prowl's resourceful answer, with permission:
根据C ++ 11标准的5.3.4/2段:
Per Paragraph 5.3.4/2 of the C++11 Standard:
如果
auto
类型说明符出现在 new-type-id 或 type-id 的 type-specifier-seq 中> new-expression , new-expression 应包含形式为new-initializer
If the
auto
type-specifier appears in the type-specifier-seq of a new-type-id or type-id of a new-expression, the new-expression shall contain a new-initializer of the form
( assignment-expression )
分配的类型是从new-initializer推导出的,如下所示:假设e
是 assignment-expression
new-initializer 和T是 new-expression 的 new-type-id 或 type-id ,则分配的类型就是该类型
在发明的声明(7.1.6.4)中为变量x
推论得出:
The allocated type is deduced from the new-initializer as follows: Let e
be the assignment-expression in
the new-initializer and T be the new-type-id or type-id of the new-expression, then the allocated type is the type
deduced for the variable x
in the invented declaration (7.1.6.4):
T x(e);
[示例:
new auto(1); // allocated type is int
auto x = new auto(’a’); // allocated type is char, x is of type char*
— 示例]
这篇关于新汽车会做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!