Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to find an enum's underlying type?

172 views
Skip to first unread message

Ivan Godard

unread,
May 16, 2012, 1:37:19 PM5/16/12
to
A web search comes up with a variety of syntax:
std::underlying_type<E>::type
std::EnumeratedType<T>::underlyingType
etc. etc. None seem to work with gcc4.6.3.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Marc

unread,
May 16, 2012, 8:25:05 PM5/16/12
to
Ivan Godard wrote:

> A web search comes up with a variety of syntax:
> std::underlying_type<E>::type
> std::EnumeratedType<T>::underlyingType

That second one doesn't look at all like a standard type.

> etc. etc. None seem to work with gcc4.6.3.

Try 4.7.0 then, this is a C++11 feature.

Zoltan Juhasz

unread,
May 16, 2012, 8:25:50 PM5/16/12
to
I would guess that 'std::underlying_type' is not implemented in
libstdc++ that is shipped with gcc 4.6.3. Unfortunately the
libstdc++ status page does not provide information on a specific
release, but only on the mainline GCC SVN (see 20.9.7.6):

http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html


According to that page, 20.9.7.6 is fully supported in the mainline GCC SVN
(thus std::underlying_type as well).


The declaration is:

template <class T> struct underlying_type;

so your first example should work (at some point in the future :).


You can actually check yourself by inspecting the

[/path/to]/gcc-4.6.3/include/c++/4.6.3/type_traits

header.


PS: It does not seem to be supported in the stdlib
implementation shipped with gcc-4.6.2.

-- Zoltan

Daniel Krügler

unread,
May 16, 2012, 8:26:48 PM5/16/12
to
Am 16.05.2012 19:37, schrieb Ivan Godard:
> A web search comes up with a variety of syntax:
> std::underlying_type<E>::type

This is the correct way, assuming, you have header
<type_traits> included and E is an enumeration type

> std::EnumeratedType<T>::underlyingType

I don't know what this should refer to. I'm quite sure that
std::EnumeratedType had never been proposed. A member type named
'underlyingType' would also seem quite odd given the

> etc. etc. None seem to work with gcc4.6.3.

This may very well be true (assuming that you have included the right
header). In this case you should try gcc 4.7 or greater. Surely gcc 4.8
has this type trait, but also note that this additionally requires to
set the compiler flag -std=c++0x (or -std=c++11 from gcc 4.8 (?) on).

HTH & Greetings from Bremen,

Daniel Krügler

Marc

unread,
May 17, 2012, 9:43:42 AM5/17/12
to
Zoltan Juhasz wrote:

> I would guess that 'std::underlying_type' is not implemented in
> libstdc++ that is shipped with gcc 4.6.3. Unfortunately the
> libstdc++ status page does not provide information on a specific
> release, but only on the mainline GCC SVN (see 20.9.7.6):
>
> http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html

You can access the documentation of each release:
http://gcc.gnu.org/onlinedocs/gcc-4.6.0/libstdc++/manual/manual/status.html#
status.iso.200x
(says underlying_type is missing)

For 4.6.3, it seems to say it is present, which looks like a bug.
0 new messages