The extern "C" construct - does it allow the use of C-style (int-based) enums?

C

Chris Cahoon

Hello,

I am trying to include a header file which was written in C, but my
project is in C++. Even when I wrap the include statement or the
entire header files with extern "C" (used according to the
instructions on c++-faq-lite), I get the same error, which is this:

invalid conversion from `int' to `DmtxDirection'

where DmtxDirection is an enum with integer values inside (obviously).

The offending function:

static DmtxDirection
TurnCorner(DmtxDirection dir, int turn)
{
DmtxDirection newDir;

newDir = (turn == DMTX_TURN_CW) ? 0x0f & (dir >> 1) : 0x0f & (dir
<< 1);

return (newDir) ? newDir : (dir ^ 0x0f) & 0x09;
}

And the enum:

typedef enum {
DmtxDirNone = 0x00,
DmtxDirUp = 0x01 << 0,
DmtxDirLeft = 0x01 << 1,
DmtxDirDown = 0x01 << 2,
DmtxDirRight = 0x01 << 3,
DmtxDirHorizontal = DmtxDirLeft | DmtxDirRight,
DmtxDirVertical = DmtxDirUp | DmtxDirDown,
DmtxDirRightUp = DmtxDirRight | DmtxDirUp,
DmtxDirLeftDown = DmtxDirLeft | DmtxDirDown
} DmtxDirection;


Long story short - should the extern "C" construct allow the library
to use enums in this manner, or do I need to take a different course?

Thanks for any advice.

Chris
 
D

Dizzy

Chris said:
Long story short - should the extern "C" construct allow the library
to use enums in this manner, or do I need to take a different course?

It does not. extern "C" doesn't alter the behaivour of not allowing implicit
conversion from int to enum. However, you can use static_cast for that,
static_cast<DmtxDirection>(value).
 
D

Davlet Panech

Chris said:
Hello,

I am trying to include a header file which was written in C, but my
project is in C++. Even when I wrap the include statement or the
entire header files with extern "C" (used according to the
instructions on c++-faq-lite), I get the same error, which is this:

invalid conversion from `int' to `DmtxDirection'

where DmtxDirection is an enum with integer values inside (obviously).

extern "C" changes "linkage" of functions and variables (i.e., it
affects the symbol names in the object files). It doesn't affect enums
in any way. You might want to add a type cast from int to enum to your code:
The offending function:

static DmtxDirection
TurnCorner(DmtxDirection dir, int turn)
{
DmtxDirection newDir;

newDir = (turn == DMTX_TURN_CW) ? 0x0f & (dir >> 1) : 0x0f & (dir
<< 1);

newDir = static_cast <DmtxDirection> ((turn = DMTX_TURN_CW ...


D.
 
J

James Kanze

extern "C" changes "linkage" of functions and variables (i.e., it
affects the symbol names in the object files). It doesn't affect enums
in any way. You might want to add a type cast from int to enum to your code:
newDir = static_cast <DmtxDirection> ((turn = DMTX_TURN_CW ...

Which will cause a few problems when the header is compiled as
C:). If he has access to the header, and can modify it, then
he can use a C style cast here. If he doesn't, he'll need to
"pre-process" the header somehow, and include the pre-processed
version. (If he's using make, it's almost trivial to get the
system to invoke the preprocessing automatically. If he's using
an IDE, I don't know.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top