C++打包 typedef 枚举

发布于 2024-09-01 02:50:28 字数 1033 浏览 8 评论 0 原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

貪欢 2024-09-08 02:50:28

更新:

对于 C++11 及更高版本,您可以指定 enum 的基础类型。例如:

enum BeNeLux : uint8_t {
   BELGIUM,
   NETHERLANDS,
   LUXEMBURG
};

但这仅适用于代码仅为 C++ 的情况。如果代码需要与 C 和 C++ 兼容,我相信我原来的答案仍然适用。


我认为这里没有什么东西可以完全满足您的需求。我假设您正在尝试创建一个枚举范围的最小类型的类型。

如果您需要这种类型的控件,我会推荐这样的东西:

typedef unsigned char BeNeLux;
static const BeNeLux BELGIUM = 0;
static const BeNeLux NETHERLANDS = 1;
static const BeNeLux LUXEMBURG = 2;

不太漂亮,而且可能类型安全性稍差。但有你想要的效果。 sizeof(BeNeLux) == 1 并且您拥有该范围内所有值的命名常量。只要您从不尝试使用变量的地址,一个好的编译器甚至不会为 static const 整数值分配变量。

UPDATE:

For C++11 and later, you can specify the underlying type of enums. For example:

enum BeNeLux : uint8_t {
   BELGIUM,
   NETHERLANDS,
   LUXEMBURG
};

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:

typedef unsigned char BeNeLux;
static const BeNeLux BELGIUM = 0;
static const BeNeLux NETHERLANDS = 1;
static const BeNeLux LUXEMBURG = 2;

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 for static const integer values so long as you never attempt to use the address of it.

风吹雨成花 2024-09-08 02:50:28
#if defined (__GNUC__)
#  if defined (__cplusplus)
#     define _ASSOCIATIONS_(X) __attribute__((packed))
#  else
#     define _ASSOCIATIONS_(X) __attribute__((packed)) X
#  endif
#else
#  if defined (__cplusplus)
#     define _ASSOCIATIONS_(X)
#  else
#     define _ASSOCIATIONS_(X) X
#  endif
#endif

typdef enum BeNeLux {
  BELGIUM,
  NETHERLANDS,
  LUXEMBURG
} _ASSOCIATIONS_ (BeNeLux);

这似乎可以在我的 g++ (GCC) 4.2.4 (Ubuntu 4.2.4-1ubuntu4) 中编译

#if defined (__GNUC__)
#  if defined (__cplusplus)
#     define _ASSOCIATIONS_(X) __attribute__((packed))
#  else
#     define _ASSOCIATIONS_(X) __attribute__((packed)) X
#  endif
#else
#  if defined (__cplusplus)
#     define _ASSOCIATIONS_(X)
#  else
#     define _ASSOCIATIONS_(X) X
#  endif
#endif

typdef enum BeNeLux {
  BELGIUM,
  NETHERLANDS,
  LUXEMBURG
} _ASSOCIATIONS_ (BeNeLux);

This seems to compile in my g++ (GCC) 4.2.4 (Ubuntu 4.2.4-1ubuntu4)

爱情眠于流年 2024-09-08 02:50:28

这里没有真正的突破。我只是重新排序了一些东西,希望你能
编译器会更喜欢它。我没有你的 gcc 或 g++ 版本,所以我无法使用它们进行测试。我确实通过版本 3.4.5 gcc 和 g++ (mingw 特殊)运行了它,当像您的代码一样订购时,它们都会警告 'packed' attributeignored ,但没有抱怨我的。

#ifdef __GNUC__
#define attribute(x) __attribute__((x));
#else
#define attribute(x)
#endif


#ifdef __cplusplus
extern "C" {
#endif

enum BeNeLux
{
    BELGIUM,
    NETHERLANDS,
    LUXEMBURG
} attribute(packed);
// I declared attribute to look like a f() so that it would not look like I was
// declaring a variable here.

#ifndef __cplusplus
typedef enum BeNeLux BeNeLux; // the typedef is separated into a separate stmt
#else
}
#endif

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.

#ifdef __GNUC__
#define attribute(x) __attribute__((x));
#else
#define attribute(x)
#endif


#ifdef __cplusplus
extern "C" {
#endif

enum BeNeLux
{
    BELGIUM,
    NETHERLANDS,
    LUXEMBURG
} attribute(packed);
// I declared attribute to look like a f() so that it would not look like I was
// declaring a variable here.

#ifndef __cplusplus
typedef enum BeNeLux BeNeLux; // the typedef is separated into a separate stmt
#else
}
#endif
热风软妹 2024-09-08 02:50:28

在 C++ 中,您不需要 typedef。只需从 enum BeNeLux 开始。也有可能(我永远不记得)声明具有相同标识符的类型和变量在其中一种语言中可能不合法。

In C++, you don't need the typedef. Just start with enum 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.

棒棒糖 2024-09-08 02:50:28
enum BeNeLux
{
   BELGIUM,
   NETHERLANDS,
   LUXEMBURG
};

这是 C++ 代码所期望的。

enum BeNeLux
{
   BELGIUM,
   NETHERLANDS,
   LUXEMBURG
};

This is what is expected of C++ code.

人事已非 2024-09-08 02:50:28

我想你正在使用 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.

情释 2024-09-08 02:50:28
#if defined (__GNUC__)

#  if defined (__cplusplus)
#     define ENUM enum
#  else
#     define ENUM typedef enum
#  endif

#  if defined (__cplusplus)
#     define _ASSOCIATIONS_(X) __attribute__((packed))
#  else
#     define _ASSOCIATIONS_(X) __attribute__((packed)) X
#  endif
#else
#  if defined (__cplusplus)
#     define _ASSOCIATIONS_(X)
#  else
#     define _ASSOCIATIONS_(X) X
#  endif
#endif

ENUM BeNeLux {
  BELGIUM,
  NETHERLANDS,
  LUXEMBURG
} _ASSOCIATIONS_ (BeNeLux);

另一个片段。查看新的#define ENUM

#if defined (__GNUC__)

#  if defined (__cplusplus)
#     define ENUM enum
#  else
#     define ENUM typedef enum
#  endif

#  if defined (__cplusplus)
#     define _ASSOCIATIONS_(X) __attribute__((packed))
#  else
#     define _ASSOCIATIONS_(X) __attribute__((packed)) X
#  endif
#else
#  if defined (__cplusplus)
#     define _ASSOCIATIONS_(X)
#  else
#     define _ASSOCIATIONS_(X) X
#  endif
#endif

ENUM BeNeLux {
  BELGIUM,
  NETHERLANDS,
  LUXEMBURG
} _ASSOCIATIONS_ (BeNeLux);

Another snippet. see the new #define ENUM

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文