[std-proposals] Is there any proposals on providing tuple-like access for enums?

86 views
Skip to first unread message

Sam Kellett

unread,
Jul 29, 2016, 4:47:45 AM7/29/16
to std-pr...@isocpp.org
I had an idea that is akin to std::tuple_element and std::tuple_size although it would obviously require compiler magic rather than a library implementation.

An example:

enum class foo {
  bar, baz, quux
};

static_assert(std::enum_size<foo>::value == 3);
static_assert(std::enum_element<0, foo>::value == foo::bar);
static_assert(std::enum_element<1, foo>::value == foo::baz);
static_assert(std::enum_element<2, foo>::value == foo::quux);

szollos...@gmail.com

unread,
Jul 29, 2016, 5:25:00 AM7/29/16
to ISO C++ Standard - Future Proposals
Hi,

If this happens, I'd suggest also adding begin() and end() and operator++ for enums, for this makes them almost trivial and would allow for range-based for() loops over non-sequential enums.
Btw, would the first parameter of std::enum_element<> mean value assigned or position (or would we have both)?

Thanks,
-lorro

Sam Kellett

unread,
Jul 29, 2016, 5:36:30 AM7/29/16
to std-pr...@isocpp.org
On 29 July 2016 at 10:25, <szollos...@gmail.com> wrote:
Hi,

If this happens, I'd suggest also adding begin() and end() and operator++ for enums, for this makes them almost trivial and would allow for range-based for() loops over non-sequential enums.
Btw, would the first parameter of std::enum_element<> mean value assigned or position (or would we have both)?

position, ala std::tuple_element.

Ricardo Fabiano de Andrade

unread,
Jul 29, 2016, 8:45:19 AM7/29/16
to std-pr...@isocpp.org
Please take a look at the current proposal for static (compile-time) reflection: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0194r1.html
Once in the standard you will be able to achieve such functionality by doing:
using meta_foo = reflexpr(foo); // obtains foo's meta-object (internal) representation
static_assert(std::meta::get_size_v<meta_foo> == 3);
static_assert(std::meta::reflects_same_v<std::meta::get_element_m<0, meta_foo>, reflexpr(foo::bar));
static_assert(std::meta::reflects_same_v<std::meta::get_element_m<1, meta_foo>, reflexpr(foo::baz));
static_assert(std::meta::reflects_same_v<std::meta::get_element_m<2, meta_foo>, reflexpr(foo::quux));

--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAHK%2B-Fu051CUsD-u%3D%3DXYA5VmEx9ubJTtVQvnNkJ9_XjVTj7qmQ%40mail.gmail.com.

Sam Kellett

unread,
Jul 29, 2016, 10:35:44 AM7/29/16
to std-pr...@isocpp.org
On 29 July 2016 at 13:45, Ricardo Fabiano de Andrade <ricardofabi...@gmail.com> wrote:
Please take a look at the current proposal for static (compile-time) reflection: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0194r1.html
Once in the standard you will be able to achieve such functionality by doing:
using meta_foo = reflexpr(foo); // obtains foo's meta-object (internal) representation
static_assert(std::meta::get_size_v<meta_foo> == 3);
static_assert(std::meta::reflects_same_v<std::meta::get_element_m<0, meta_foo>, reflexpr(foo::bar));
static_assert(std::meta::reflects_same_v<std::meta::get_element_m<1, meta_foo>, reflexpr(foo::baz));
static_assert(std::meta::reflects_same_v<std::meta::get_element_m<2, meta_foo>, reflexpr(foo::quux));

yeah that's all great and all but realistically how far away is that from being used in production code? if tuple's were allowed introspection earlier, why not enums too?
 

Ricardo Fabiano de Andrade

unread,
Jul 29, 2016, 11:28:51 AM7/29/16
to std-pr...@isocpp.org
There were no core language changes to support obtaining that info from a tuple.
What you see is just pure meta-programming in action -- of course, with support of type traits which offers some level of type introspection.
What you're asking would certainly require actual introspection. That's what the reflection proposal aims to offer.

And we're not going to get anything before C++20 anyways. By there, hopefully we're going to have the proposal voted into the standard.
If you want it faster (maybe as a TS), you can always volurteer yourself to help: https://groups.google.com/a/isocpp.org/forum/#!forum/reflection


--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.

Nicol Bolas

unread,
Jul 29, 2016, 1:53:22 PM7/29/16
to ISO C++ Standard - Future Proposals

The usual: 3-5 years. But whatever you propose would be 3-5 years too, since it certainly isn't going into C++17, which is feature complete.

if tuple's were allowed introspection earlier, why not enums too?

Because you can't do anything with a tuple without the ability to get a value from it.
Reply all
Reply to author
Forward
0 new messages