Skip to content

## 第二章 2.3类型推导中的一个疑问 #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
guojinyu12 opened this issue Jul 28, 2020 · 4 comments
Closed

## 第二章 2.3类型推导中的一个疑问 #111

guojinyu12 opened this issue Jul 28, 2020 · 4 comments
Labels

Comments

@guojinyu12
Copy link

Motivation

Please briefly describe your motivation.

Requirements

Please list all of your suggestions.


动机

请描述你提交内容建议的动机。

需求说明

请描述你提交内容建议的详单,例如具体是增加哪个知识点的说明。

@guojinyu12
Copy link
Author

初次尝试提出问题,如果对任何人造成困扰,非常抱歉。

第二章 2.3节类型推导,auto小节中有如下示例:

auto auto_arr2[10] = arr;   // 错误,  无法推导数组元素类型

2.6.auto.cpp:30:19: error: 'auto_arr2' declared as array of 'auto'
    auto auto_arr2[10] = arr;

联系上下文得知,我猜测arr指同一小节如下文本:

auto i = 5;              // i 被推导为 int
auto arr = new auto(10); // arr 被推导为 int *

动机

那么,在arr是一个变量的情况下,请问将变量赋值给数组的行为本身合法吗?这里是不是存在第二个错误?

int* auto_arr2[10] = arr;

即使是改为上面的行为就合法吗?

需求说明

下面的写法不应该更加形象吗?更能指明主旨,auto不能推导数组类型。

auto auto_arr2[10] = {arr};

@guojinyu12 guojinyu12 reopened this Jul 28, 2020
@guojinyu12 guojinyu12 changed the title ## 动机 ## 第二章 2.3类型推导中的一个疑问 Jul 28, 2020
@tinyvan
Copy link
Contributor

tinyvan commented Jul 31, 2020

int* auto_arr2[10] = arr;

这行代码是错误的,auto_arr2[10]的元素类型是int而非int*,且数组必须用括号封闭初始器初始化,所以应为

int auto_arr2[10] = {arr};  //合法
auto auto_arr2[10] = {arr);

我赞同添加括号,已提PR #112

@changkun
Copy link
Owner

感谢楼上各位的报告,没错,应该使用 {...} 来初始化 aggregate object

@changkun changkun added the bug label Aug 10, 2020
@changkun
Copy link
Owner

Fixed in #112

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants