C ++ typedef enum packing

typedef enum BeNeLux
{
   BELGIUM,
   NETHERLANDS,
   LUXEMBURG
} _ASSOCIATIONS_ BeNeLux;

When I try to compile this using the C ++ compiler, I get errors, but it seems to work fine with the C compiler. So here is the question. Is it possible to package an enumeration in C ++, or can anyone understand why I got the error?

Error:

"after the declaration BeNeLux with a comma is missing.

I know after checking and re-checking that there is definitely a semicolon and in any places required in the rest of the code.

Application:

_PACKAGE_was just an example. I rename it.

_ASSOCIATIONS_ not a BeNeLux type:

#define _ASSOCIATIONS_ __attribute__((packed))

The iffed code, but only to make sure it is GNU C / C ++.

#if defined (__GNUC__) 
#define _ASSOCIATIONS_ __attribute__((packed))
#else
#define _ASSOCIATIONS_

Will 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

There is no joy. Is anyone

: -fshort-enums ; .

+5
7

, -, , . , , .

, - :

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

, , . , . sizeof(BeNeLux) == 1, . static const integer, .

+1
#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)

+1

. , . gcc g++, . 3.4.5 gcc g++ (mingw special), 'packed' attribute ignored , , .

#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
+1

, GCC g++. . , ++ .

, #define #if ' ++, . #ifndef __cplusplus.

g++ -E, , #define.

, , , . , PACKED .

0

++ typedef. enum BeNeLux. ( ), .

0
enum BeNeLux
{
   BELGIUM,
   NETHERLANDS,
   LUXEMBURG
};

, ++.

0
#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

0

All Articles