-1

I have code that contains the line

enum struct cols: int8_t {red, blue, green};

When i compile this, i get errors:

test.cpp:4:1: warning: elaborated-type-specifier for a scoped enum must not use the 'struct' keyword
 enum struct cols: int8_t {red, blue, green};
 ^
test.cpp:4:13: error: use of enum 'cols' without previous declaration
 enum struct cols: int8_t {red, blue, green};
             ^
test.cpp:4:17: error: expected unqualified-id before ':' token
 enum struct cols: int8_t {red, blue, green};
                 ^

However if i put the line

#include <iostream>

at the top, it compiles without complaint.

Is there an explanation for this?

(I am using g++ 4.9.4, but this behaviour is also displayed with g++ 5.4.0.)

underscore_d
  • 6,309
  • 3
  • 38
  • 64
user1479670
  • 1,145
  • 3
  • 10
  • 22

1 Answers1

1

std::int8_t is not a built-in type. It is, like all other exact-width types, an optional typedef to a built-in type, present only if your system has an appropriate type of that width. This and other available std::[u]int*_t types are defined in <cstdint>. Therefore, you need to #include <cstdint>.

As my above paragraph indicates, you should also specify the std:: namespace qualifier, as stdlib symbols in the <c*> headers are not required to be made available in the global namespace.

Presumably <iostream> was previously indirectly including <cstdint> by some route, but you should not rely on that; you should #include the correct header for every library symbol you use.

Then the thing about struct is a red herring arising from the other, main problem of the unknown underlying type; see Elaborated-type-specifier for a scoped enum must not use the ‘class’ keyword, which now that I look at it, is nearly an exact duplicate of your question.

underscore_d
  • 6,309
  • 3
  • 38
  • 64