Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C++ use a C bitmask (anonymous enum) exposed by iOS native library [closed]

Cocoa utilises typedef-ed anonymous enum bitfields.

I'm using objective-C++, for better & worse. Inside a .mm file I need to assign 2 bits (bitwise inclusive OR) to a property of the type of one of these enum bitfield types. The libc++ compiler won't have it because it won't give an rvalue of type int to a property of that typedef-ed anonymous enum bitfield.

I understand there is a size difference of enums between C & C++. So what is the work-around for this situation?

My line performing the assignment is akin to:

    uiSwipeRightDownRecogniser.direction = Right | Down;

The definition of the bitfield is akin to:

    typedef enum 
    {
        Right = 1 << 0,
        Left  = 1 << 1,
        Up    = 1 << 2,
        Down  = 1 << 3
    } UISwipeDirection;

The error is:

Cannot initialize a parameter of type 'UISwipeDirection' with an rvalue of type 'int'

That kind of assignment works in a .m file, but not a .mm.

The compiler is Apple's LLVM 3.0 (using libc++).

like image 903
codey Avatar asked Nov 14 '22 14:11

codey


1 Answers

Just convert it using static_cast:

uiSwipeRightDownRecogniser.direction = static_cast<UISwipeDirection>(Right | Down);
like image 121
justin Avatar answered Dec 18 '22 01:12

justin