gpt4 book ai didi

c++ - 可以将变量重新声明为推断为相同类型的 auto 吗?

转载 作者:IT老高 更新时间:2023-10-28 22:22:40 26 4
gpt4 key购买 nike

标准是否允许以下​​内容?

#include <iostream>

extern int a;
auto a = 3;

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

clang接受代码。 g++提示声明冲突。

最佳答案

从标准上我不太清楚,但是,有这样写的

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

最好阅读标准中提到的所有允许上下文的部分。

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

关于c++ - 可以将变量重新声明为推断为相同类型的 auto 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37407547/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com