- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的头文件中声明了以下代码:
//Integer Vector4D stuffs
typedef union {
size_t data[4];
struct {
size_t x;
size_t y;
size_t z;
size_t w;
};
struct {
size_t x;
size_t y;
size_t width;
size_t height;
};
} Vector4Di;
//End Integer Vector4D stuffs
我正在为 Windows 和 Linux 编译代码。我正在使用带有 WSL 的 Windows 10 专业版。
这是我从 Microsoft Visual Studio 2017 获取的编译生成输出:
1>------ Build started: Project: SDL, Configuration: Debug x64 ------
1>C:main_pc.cpp
1>1 File(s) copied
1>block.cpp
1>common.cpp
1>draw.cpp
1>game.cpp
1>input.cpp
1>main.cpp
1>Generating Code...
1>SDL.vcxproj -> C:\Users\tom_mai78101\Documents\VSProjects\SDL\x64\Debug\SDL.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
从上面的构建日志可以看出,编译时构建成功。
但是,相同的源代码会在 GCC 中引发错误:
$ make
clean ...
build ...
main.cpp
In file included from /mnt/c/Users/tom_mai78101/Documents/VSProjects/SDL/SDL/../SDL/main.cpp:1:0:
/mnt/c/Users/tom_mai78101/Documents/VSProjects/SDL/SDL/../SDL/game/common.h: At global scope:
/mnt/c/Users/tom_mai78101/Documents/VSProjects/SDL/SDL/../SDL/game/common.h:358:10: error: redeclaration of 'size_t <unnamed union>::<unnamed struct>::x'
size_t x;
^
/mnt/c/Users/tom_mai78101/Documents/VSProjects/SDL/SDL/../SDL/game/common.h:352:10: note: previous declaration 'size_t <unnamed union>::<unnamed struct>::x'
size_t x;
^
/mnt/c/Users/tom_mai78101/Documents/VSProjects/SDL/SDL/../SDL/game/common.h:359:10: error: redeclaration of 'size_t <unnamed union>::<unnamed struct>::y'
size_t y;
^
/mnt/c/Users/tom_mai78101/Documents/VSProjects/SDL/SDL/../SDL/game/common.h:353:10: note: previous declaration 'size_t <unnamed union>::<unnamed struct>::y'
size_t y;
^
要点是,在 GCC 中,它会提示 size_t x;
和 size_t y;
如何在 union 内重新声明,Vector4Di
。但是 MSBuild 不会抛出这方面的错误,它成功编译了代码。
据我所知,我认为具有相同类型和相同变量名的 union 不应相互冲突,尤其是当变量位于结构中时。特别是在为 C++11 编译时,众所周知 C++11 对 union 有更好的支持。
请问为什么它会抛出 GCC 错误?我应该如何修复此错误,以便 MSBuild 和 GCC 都能成功编译?
提前致谢。
最佳答案
C++ 标准不允许匿名 struct
不被用来在 union
中定义其类型的成员;使用匿名 struct
将其成员注入(inject)封闭的 union
is just a widespread extension ,但它不是标准的 C++,因此并未严格定义该机制的工作原理。
特别是,在这里您正踏入危险的水域,因为您有两个匿名结构,其成员具有相同的名称;从 C++ 标准的角度来看,这绝对是模糊的,因为当你这样做时,什么是“活跃的 union 成员”是模棱两可的。 v4d.x = 5;
.
VC++ documentation没有描述在这种情况下会发生什么;快速检查告诉我 it doesn't get mad even if the names refer to conflicting memory locations - 它似乎只是指第一个被定义的匿名结构的成员,如果你问我,这完全是疯了。
gcc 反而会生气,因为它就像在同一个 struct
中声明两个具有相同名称的成员; its documentation实际上说:
You must never create such structures that cause ambiguous field definitions. For example, in this structure:
struct {
int a;
struct {
int a;
};
} foo;it is ambiguous which a is being referred to with
foo.a
. The compiler gives errors for such constructs.
因此,这不是您可以通过编译器开关或其他方式解决的问题; gcc 不支持它,期间。
这一愿景得到了唯一正式正式描述此功能的标准的确认,即 ISO C 11 标准;在那里,在 §6.7.2.1 ¶13,据说:
An unnamed member of structure type with no tag is called an anonymous structure; an unnamed member of union type with no tag is called an anonymous union. The members of an anonymous structure or union are considered to be members of the containing structure or union. This applies recursively if the containing structure or union is also anonymous.
结构的命名成员必须具有唯一的名称。
所以,回顾一下:
如果你想让它成为标准的 C++,你必须给你的子结构起一些名字;你可以这样做:
union Vector4Di {
size_t data[4];
struct {
size_t x;
size_t y;
size_t z;
size_t w;
} q;
struct {
size_t x;
size_t y;
size_t width;
size_t height;
} r;
};
调整其余代码并对其感到满意。
如果您可以使用这些扩展,您仍然可以通过在第二个 struct< 中以不同方式命名
,如:x
和 y
来使其正常工作
union Vector4Di {
size_t data[4];
struct {
size_t x;
size_t y;
size_t z;
size_t w;
};
struct {
size_t dummy_x;
size_t dummy_y;
size_t width;
size_t height;
};
};
在这种情况下,您可以自由地使用 x
和 y
以及 width
和 height
,我怀疑从标准的角度来看,它甚至还可以——如果我们忽略“匿名结构”这个东西;这两个结构是标准布局并且与布局兼容,因此标准保证您可以使用您喜欢的任何名称来检查“公共(public)初始序列”。
关于c++ - 相同类型和相同变量的 union 在 GCC 中抛出错误,但在 Visual C++ 中不会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49602960/
我正在尝试在 Conda 环境中编译一些代码,在那里我 之前安装的编译包gcc_linux-64 . 然而,即使在停用和重新激活环境之后,gcc还在/usr/bin/gcc . 我该怎么做才能让 Co
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 7 年前。 Improve
这其实是两个问题: 1 - 在我的 debian amd64 系统上,我似乎无法构建与 gmp/mpfr/mpc 动态链接的交叉 GCC。即使我删除 --disable-shared,它也总是静态链接
研究ELF格式的结果,可以看到目标文件中有一个符号对应每个函数,对应的符号表项的值为st_size,表示大小的功能。 问题是,即使我更改了目标文件中特定函数的 st_size 并链接了它,但可执行文件
海湾合作委员会的 documentation for #line directives说他们是这样的: #line "myfile.cpp" 123 但是当我用 g++ 5.1 检查输出时,它们实际上
我正在使用 as 和 gcc 来汇编和创建 ARM 汇编程序的可执行文件,正如 this 所推荐的那样教程,如下: 给定一个汇编源文件,program.s,我运行: as -o program.o p
long long x; double n; x=long long(n); 这不起作用。什么是正确的方法? 最佳答案 显而易见的: x = (long long) n; 关于gcc - 转换为长长
我想知道用于 gcc 的原子内置函数的头文件是什么? 我想使用这 2 个函数为我当前创建的线程库实现互斥锁。 bool __sync_bool_compare_and_swap (type *ptr,
它出现在 another question :gcc调用的程序和部件是什么? (特别是在编译 C 或 C++ 时)以便有人可以设计一些拦截和更改流程的方案以用于各种自定义编码目的? 最佳答案 编译器二
可能吗?我想使用 gcc喜欢 assembler并在将其编译为 ubuntu 上的可执行文件后。 我尝试过这个: gcc a.asm -o out.o 来自 out.o文件编译成.out可执行文件。
我写了一个简单的 C 程序 test.c : #include #include int add(int a, int b); int main() { int i=5,j=10;
即。所以如果你使用任何八进制文字,它会给你一个警告。 微软编译器的同样问题。 如果没有,是否有任何其他工具可以检测八进制文字。 (vim 似乎有一个很酷的技巧,它突出了第一个领先的将不同的颜色归零,但
我在旧线程中搜索。但没有找到任何线程回答我的问题。 gcc 是否像 vc++ 一样支持函数级链接? 如果是,我应该提供什么选项来链接目标文件和库? 最佳答案 看起来 gcc 不直接支持函数级链接。您可
也许标题并没有把问题说得那么准确:我知道当我运行 gcc foo.c 时,GCC 会调用其他为它完成所有工作的子程序,从而生成主 gcc 程序只是一个界面。但这究竟是如何完成的呢? 它是否使用syst
我听说最近版本的 gcc 非常擅长将通过函数指针的调用转换为直接调用。但是,我在网上或快速浏览 gcc 的源代码上找不到任何关于它的信息。有谁知道这是否真的是真的,如果是这样,它使用什么算法来做到这一
gcc/g++ 链接器选项“-Map”生成的“.map”文件用于什么? 以及如何阅读它们? 最佳答案 我建议为您投入生产的任何软件生成一个映射文件并保留一份副本。 它可用于破译崩溃报告。根据系统的不同
gcc信息文件在有关x86-64特定标志的部分中说 其他事情: There is no `-march=generic' option because `-march' ind
我想知道 gcc 链接器选项(例如:-Wl,options)是否可以更改编译后的可执行文件中的汇编指令,因为如果您使用某些 gcc 优化选项会发生这种情况? 当您比较编译后的二进制文件(例如比较签名)
是否有GCC编译指示会停止,暂停或中止编译过程? 我正在使用gcc 4.1,但也希望在gcc 3.x版本上也可以使用该编译指示。 最佳答案 您可能需要#error: edd@ron:/tmp$ g++
当我使用gcc编译C程序时我通常使用 -g 将一些调试信息放入 elf 文件中这样 gdb 就可以在需要时帮助我。 但是,我注意到有些程序使用 -ggdb,因为它应该使调试信息对 gdb 更加友好。
我是一名优秀的程序员,十分优秀!