C++打包 typedef 枚举
typedef enum BeNeLux
{
BELGIUM,
NETHERLANDS,
LUXEMBURG
} _ASSOCIATIONS_ BeNeLux;
当我尝试使用 C++ 编译器编译它时,我收到错误,但它似乎与 C 编译器一起工作正常。那么问题来了。是否可以在 C++ 中打包枚举,或者有人能明白为什么我会收到错误吗?
错误是:
“声明 BeNeLux 后缺少分号”。
经过检查和重新检查后,我知道那里肯定有一个分号,并且在其余代码所需的任何地方都有分号。
附录:
_PACKAGE_
只是一个示例。我正在重命名它。
_ASSOCIATIONS_
不是 BeNeLux 的类型:
#define _ASSOCIATIONS_ __attribute__((packed))
代码已验证,但只是为了确保它是 GNU C/C++。
#if defined (__GNUC__)
#define _ASSOCIATIONS_ __attribute__((packed))
#else
#define _ASSOCIATIONS_
这会引起问题吗?我认为 (GNUC) 对 C 和 C++ 都有效
附录 2:
我什至尝试过
#ifdef __cplusplus
extern "C" {
#endif
typedef enum BeNeLux
{
BELGIUM,
NETHERLANDS,
LUXEMBURG
} _ASSOCIATIONS_ BeNeLux;
#ifdef __cplusplus
}
#endif
没有乐趣。有人吗?
注意:-fshort-enums 是不可能的;寻找程序化解决方案。
typedef enum BeNeLux
{
BELGIUM,
NETHERLANDS,
LUXEMBURG
} _ASSOCIATIONS_ BeNeLux;
When I try to compile this with C++ Compiler, I am getting errors, but it seems to work fine with a C compiler. So here's the question. Is it possible to pack an enum in C++, or can someone see why I would get the error?
The error is:
"semicolon missing after declaration of BeNeLux".
I know, after checking and rechecking, that there definitely is a semicolon there, and in any places required in the rest of the code.
Addendum:
_PACKAGE_
was just an example. I am renaming it.
_ASSOCIATIONS_
is not a type of BeNeLux:
#define _ASSOCIATIONS_ __attribute__((packed))
The code is iffed, but only to make sure it is GNU C/C++.
#if defined (__GNUC__)
#define _ASSOCIATIONS_ __attribute__((packed))
#else
#define _ASSOCIATIONS_
Would this cause problems? I thought (GNUC) worked for both C and C++
Addendum 2:
I even tried
#ifdef __cplusplus
extern "C" {
#endif
typedef enum BeNeLux
{
BELGIUM,
NETHERLANDS,
LUXEMBURG
} _ASSOCIATIONS_ BeNeLux;
#ifdef __cplusplus
}
#endif
No joy. Anyone?
Note: -fshort-enums is not a possibility; looking for a programmatic solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
更新:
对于 C++11 及更高版本,您可以指定
enum
的基础类型。例如:但这仅适用于代码仅为 C++ 的情况。如果代码需要与 C 和 C++ 兼容,我相信我原来的答案仍然适用。
我认为这里没有什么东西可以完全满足您的需求。我假设您正在尝试创建一个枚举范围的最小类型的类型。
如果您需要这种类型的控件,我会推荐这样的东西:
不太漂亮,而且可能类型安全性稍差。但有你想要的效果。
sizeof(BeNeLux) == 1
并且您拥有该范围内所有值的命名常量。只要您从不尝试使用变量的地址,一个好的编译器甚至不会为static const
整数值分配变量。UPDATE:
For C++11 and later, you can specify the underlying type of
enum
s. For example:But this only applies if the code will be C++ only. If the code needs to be compatible with both C and C++, I believe my original answer still applies.
I don't think that there is something that does exactly what you want here. I assume you are trying to create a type that is the smallest type for the enum's range.
If you need this type of control, I would recommend something like this:
not quite as pretty and possibly a little less type safe. But has the effect that you want.
sizeof(BeNeLux) == 1
and you have a named constant for all values in the range. A good compiler won't even allocate a variable forstatic const
integer values so long as you never attempt to use the address of it.这似乎可以在我的
g++ (GCC) 4.2.4 (Ubuntu 4.2.4-1ubuntu4) 中编译
This seems to compile in my
g++ (GCC) 4.2.4 (Ubuntu 4.2.4-1ubuntu4)
这里没有真正的突破。我只是重新排序了一些东西,希望你能
编译器会更喜欢它。我没有你的 gcc 或 g++ 版本,所以我无法使用它们进行测试。我确实通过版本 3.4.5 gcc 和 g++ (mingw 特殊)运行了它,当像您的代码一样订购时,它们都会警告
'packed' attributeignored
,但没有抱怨我的。There are no real breakthroughs here. I just reordered things in the hopes that your
compiler would like it better. I did not have your version of gcc or g++ so I could not test with those. I did run it through version 3.4.5 gcc and g++ (mingw special) which both warned
'packed' attribute ignored
when ordered like your code, but did not complain about mine.在 C++ 中,您不需要
typedef
。只需从enum BeNeLux
开始。也有可能(我永远不记得)声明具有相同标识符的类型和变量在其中一种语言中可能不合法。In C++, you don't need the
typedef
. Just start withenum BeNeLux
. It's also possible (I can never remember) that declaring a type and a variable with the same identifier may not be legal in one of the languages.这是 C++ 代码所期望的。
This is what is expected of C++ code.
我想你正在使用 GCC 和 G++。对于我来说,代码可以很好地编译。当在任何一个特定编译器上启用 C++ 时,看到这样的功能消失,将是相当令人惊讶的。
确保您的
#define
未针对C++ 代码进行#if
处理,例如#ifndef __cplusplus
。尝试
g++ -E
查看预处理器输出并验证#define
是否出现。此外,即使对于预处理器宏,以下划线和大写字母或两个下划线开头的名称也会保留给编译器和库。如果您必须有一个宏,
PACKED
可能是最好的名称。I suppose you're using GCC and G++. The code compiles fine with either, for me. It would be rather suprising to see such a feature disappear when enabling C++ on any one particular compiler.
Make sure that your
#define
is not#if
'ed out for C++ code, e.g.#ifndef __cplusplus
.Try
g++ -E
to see the preprocessor output and verify that the#define
appears.Also, even for preprocessor macros, names beginning with an underscore and capital letter or two underscores are reserved for the compiler and library. If you must have a macro,
PACKED
might be the best name for it.另一个片段。查看新的
#define ENUM
Another snippet. see the new
#define ENUM