Talk:More C++ Idioms/Type Safe Enum

Problem compiling the code as shown
I've gotten compilation errors with the code as provided (gcc 4.3.3). The compiler wouldn't finish parsing the template. By modifying the template to look like I can get it to compile. I have not gotten to full check this newer version. --RhysU (talk) 14:36, 28 April 2009 (UTC)


 * No need to test, the change you've made is correct, and what is already in the article is incorrect. (The compiler already knows that Inner is a typename because it is declared as such in the template.)


 * I'll update the article now.


 * 121.210.170.141 (talk) 04:11, 26 November 2009 (UTC)

Non-member operators for left-most argument conversions
Because of the current implementation, compilers cannot do automatic conversion of the left-most arguments included in comparison.

I'm using my own implementation that defines non-member operators, and it works. If we can agree on this, I can post my code here for revision.

--VedranOdGradista (talk) 12:32, 5 March 2012 (UTC)


 * I second the request of using non-members operator.
 * --Paercebal (discuss • contribs) 13:29, 27 February 2013 (UTC)
 * --Paercebal (discuss • contribs) 13:29, 27 February 2013 (UTC)


 * No answer since more than a month. I'll do the change.
 * -- Paercebal (discuss • contribs) 12:55, 8 April 2013 (UTC)
 * -- Paercebal (discuss • contribs) 12:55, 8 April 2013 (UTC)

Iterating over enumerations
While reading the paragraph about iterating, I was somewhat confused about how a def-Type like color_def should look like. As far as I see, the template safe_enum expects some definition of color_def::_begin_ and color_def::_end_, so an implementation should be given to illustrate the principle: On another note, one could work around the limitation of contiguous values by having the def class hold the array of elements, and initialize this by a braced list: The initialization of safe_enum could then calculate the size of the array by sizeof magic to ensure correct array access (or in C++11 one could substitute type[] by a std::vector ). Depending on how often this functionality is needed, one could lessen the additional verbosity by using a MACRO (even the safe_enum stuff could be put in there). --141.30.83.52 (discuss) 13:27, 26 August 2014 (UTC)

Unwanted implicit conversion from underlying type
Because of the non-explicit constructor safe_enum(type v) : val(v) {}, and type is typedefed to inner, so, for example, when inner is provided as unsigned char, we'll have an implicit conversion from unsigned char to safe_enum, which is even forbidden by plain enum! So,

need to be changed to

The code is just correct until 12:57, 8 April 2013, then Sutambe changed it to the first, AFAK wrong, version without comment. 60.191.94.119 (discuss) 13:24, 9 June 2015 (UTC)