[Boost-users] Using boost_foreach with c++ enumerations

34 views
Skip to first unread message

Tim Burgess

unread,
Aug 5, 2013, 1:49:46 PM8/5/13
to boost-users list

Hi,

 

Is it possible to use boost_foreach to iterate over the members of a c++ enum? If so, can anybody point me to an example?

 

Many thanks in advance.

 

Best wishes.

 

Tim Burgess

Raised Bar Ltd.

 

T: 01827 719822

M: 07989 486976

W: http://www.raisedbar.net

 

Eric Niebler

unread,
Aug 5, 2013, 7:37:16 PM8/5/13
to boost...@lists.boost.org
On 8/5/2013 10:49 AM, Tim Burgess wrote:
> Hi,
>
> Is it possible to use boost_foreach to iterate over the members of a c++
> enum? If so, can anybody point me to an example?

No. BOOST_FOREACH is for ranges, and an enum is not a range. There is no
way to do what you're asking because C++ doesn't have the reflection
capabilities needed to iterate through all members of an enum.

--
Eric Niebler
Boost.org
http://www.boost.org
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Larry Evans

unread,
Aug 6, 2013, 11:02:38 AM8/6/13
to boost...@lists.boost.org
On 08/05/13 18:37, Eric Niebler wrote:
> On 8/5/2013 10:49 AM, Tim Burgess wrote:
>> Hi,
>>
>> Is it possible to use boost_foreach to iterate over the members of a c++
>> enum? If so, can anybody point me to an example?
>
> No. BOOST_FOREACH is for ranges, and an enum is not a range. There is no
> way to do what you're asking because C++ doesn't have the reflection
> capabilities needed to iterate through all members of an enum.
>
Back in 2005, I hacked together something that might help.
See:

http://article.gmane.org/gmane.comp.lib.boost.devel/117229

and maybe:

https://github.com/boost-vault/Template-Metaprogramming/blob/master/range_all.zip

The range_all.hp which I've got on my local disk has comment:

//range_c for enumerations which can include all of the enumeration

I think maybe that's what's included in the range_all.zip mentioned
above.

HTH.

-regards,
Larry
Reply all
Reply to author
Forward
0 new messages