可以将推断为相同类型的变量重新声明为auto吗? [英] Can a variable be redeclared as auto that deduced to the same type?

查看:75
本文介绍了可以将推断为相同类型的变量重新声明为auto吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标准允许以下内容吗?

#include <iostream>

extern int a;
auto a = 3;

int main(int, char**)
{
    std::cout << a << std::endl;
    return 0;
}

clang 接受该代码. g ++ 抱怨声明冲突.

clang accepts the code. g++ complains for conflicting declaration.

推荐答案

从标准上对我来说还不是很清楚,但是有

Its not much clear to me from the standard, but then, there is this written

第7.1.6.4节自动说明符
在本节未明确允许的上下文中使用auto的程序格式错误.

section 7.1.6.4 auto specifier
A program that uses auto in a context not explicitly allowed in this section is ill-formed.

对于所有允许的上下文,最好阅读标准中提到的部分.

Better read the mentioned section of the standard for all the allowed contexts.

考虑到这一点,我相信g ++是正确的,而clang是错误的.但是我可能是错的,标准中可能有单独的部分可能暗示着这种情况,但我找不到它.

Considering this, I believe g++ is correct and clang is wrong. But I could be wrong, there could be some separate section in standard which might be implying this context, but I could not find it.

这篇关于可以将推断为相同类型的变量重新声明为auto吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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