Friday, March 01, 2013

Nifty Trick of the Day: cpp loop over enum

So I was trying to figure out a quick hack to loop over an enum so that I could write a simple unit test to achieve complete coverage. So google returned the following link with this solution:

http://stackoverflow.com/questions/2479746/loop-on-enumeration-values

My slightly modified solution

for(SAID_ENUM i = value1; i < static_cast<SAID_ENUM>(LAST_ENTRY+1); i = static_cast<SAID_ENUM>(i+1)){}

of course this assumes you have an extra last "end" member  to your enum
 
 

No comments: