strongly typed enums

Can strongly typed enums underlying type be floating point?

1
2
3
4
5
6
7
enum class foo : float { value = 3.0f };

int main ()
{
    std::cout << foo::value << std::endl;
    return 0;
}


Error output:

error: underlying type 'float' of 'foo' must be an integral type|
Last edited on
No, according to the C++ standard

The type-specifier-seq of an enum-base shall name an integral type;

and further

If an initializer is specified for an enumerator, the initializing value has the same type as the expression and the constant-expression shall be an integral constant expression


So neither enum-base float, nor constant expression 3.0f are allowed.
Last edited on
Thanks.

Can you please give me a link to text that you quoted?
It is section 7.2 Enumeration declarations of the C++ Standard Draft You can download it from http://www.open-std.org/jtc1/sc22/wg21/

See ISO/IEC 14882:2011 Programming Language C++ - draft
Thanks.
Topic archived. No new replies allowed.