•      Sign In
• Create Account

### #ActualBitMaster

Posted 04 April 2013 - 05:21 AM

How is casting to int a problem if the enum is defined to use int as the underlying type?

To make things completely tight one could use (not tested but documentation suggests it should work):


static inline ENUM_DrawElements operator | (ENUM_DrawElements a, ENUM_DrawElements b)
{
return static_cast<ENUM_DrawElements>(
static_cast<std::underlying_type<ENUM_DrawElements>::type>(a) |
static_cast<std::underlying_type<ENUM_DrawElements>::type>(b));
}

A bit overkill in this particular scenario, but there are cases where types have to be changed during development. Besides, it's nice to know there is something to get the base type in the general case...

### #2BitMaster

Posted 04 April 2013 - 05:19 AM

How is casting to int a problem if the enum is defined to use int as the underlying type?

To make since completely tight one could use (not tested but documentation suggests it should work):

static inline ENUM_DrawElements operator | (ENUM_DrawElements a, ENUM_DrawElements b)
{
return static_cast<ENUM_DrawElements>(
static_cast<std::underlying_type<ENUM_DrawElements>::type>(a) |
static_cast<std::underlying_type<ENUM_DrawElements>::type>(b));
}


### #1BitMaster

Posted 04 April 2013 - 04:34 AM

How is casting to int a problem if the enum is defined to use int as the underlying type?

To make since completely tight one could use (not tested but documentation suggests it should work):
static inline ENUM_DrawElements operator | (ENUM_DrawElements a, ENUM_DrawElements b)
{
return static_cast<ENUM_DrawElements>(
static_cast<std::underlying_type<ENUM_DrawElements>>(a) |
static_cast<std::underlying_type<ENUM_DrawElements>>(b));
}


PARTNERS