c++ - 与类型同名的变量 - 哪个编译器是正确的?

标签 c++ compiler-errors clang language-lawyer variable-names

在这段代码中:

typedef int foo;

struct S
{
  foo foo;
};

int main() {}

所有版本的 clang -std=c++14 都接受此代码,但是所有版本的 g++ -std=c++14 报告:

5 : error: declaration of 'foo S::foo' [-fpermissive]
foo foo;
^
1 : error: changes meaning of 'foo' from 'typedef int foo' [-fpermissive]

代码正确吗?

最佳答案

代码错误。 typedef 是现有类型的新名称。所以你不能创建一个类型名称像 foo foo; 等于 int int 的变量。

g++ -std=c++14 是正确的。

Also refer this question

关于c++ - 与类型同名的变量 - 哪个编译器是正确的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34450095/

相关文章:

c++ - 传递模板参数

java - 为什么这个java代码显示 "non-static variable this cannot be referenced from a static context"错误?

macos - opencv 用 clang 编译好,用 gcc 编译不行 os x 10.9

c++ - 为什么一个非常大的值会导致这段代码无限重复?

c++ - C++ 编译器将为函数参数执行的各种自动类型转换的完整列表是什么?

c++ - 在所有子 header 中包含基本 header 的问题

python - 如何使用 Python 将更改应用于源文件?

linker - 使 LLVM 内联库中的函数

c++ - 多个线程/进程可以在不同步的情况下同时读取/写入文件的非重叠区域吗?

c++ - Visual Studio 2010 C++ DLL 项目 - 没有输出 DLL 文件!