c++ enum convertibility to int? -


according http://www.cplusplus.com/doc/tutorial/other_data_types/ :

values of enumerated types declared enum implicitly convertible integer type int, , vice versa. in fact, elements of such enum assigned integer numerical equivalent internally, of become alias. if not specified otherwise, integer value equivalent first possible value 0, equivalent second 1, third 2, , on...

so, following idea, wanted see if following sample work:

#include <iostream> using namespace std;  enum colors {white, yellow, red, green, blue, black};  int main() {   colors a{3};   colors b{1};   colors c{a-b};   cout << c << '\n' << (c == red) << '\n' << c+20 << '\n';   return 0; } 

and works expected, if set -fpermissive flag, otherwise g++ spits out compilation errors. , flag set still presented number of warnings, this: invalid conversion ‘int’ ‘colors’

if understand documentation correctly above sample should 100% compliant standard. however, apparently according g++ not (note word "invalid").

i had idea push convertibility further , try this: colors d{yellow}; d++; (hoping d becoming red). however, time cannot make working. get:

enum.cpp:13:22: warning: no ‘operator++(int)’ declared postfix ‘++’, trying prefix operator instead [-fpermissive]    colors d{yellow}; d++;                       ^ enum.cpp:13:22: error: no match ‘operator++’ (operand type ‘colors’) 

is code in sample correct? if so, possible increment enum variable described above?

thanks in advance.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -