Ujjwal
Ujjwal

Reputation: 151

How to get the type supporting an enum

I want to write a test to detect the underlying type of a enum and that test case should be compiler-agnostic.

I cannot use std::underlying_type, __underlying_type and other compiler specific implementation

Upvotes: 2

Views: 1074

Answers (2)

doron
doron

Reputation: 28872

GCC has __underlying_type (type) as a compiler intrinsic. Intrinsics are special functions that the compiler implements internally.

Other compilers probably work in a similar way.

Upvotes: 8

pmdj
pmdj

Reputation: 23428

This is implementation-defined. For clang, it's implemented via the __underlying_type(type) compiler feature, for example. Likewise for GCC.

The main reason not to use std::underlying_type from the standard library would be in environments where there is no standard library available. (Embedded, kernel, etc.) Usually in that situation you'll be targeting a closed set of compilers, so you can just reimplement std::underlying_type for each compiler you're targeting individually, using each compiler's necessary implementation-specific features.

Upvotes: 6

Related Questions