Let's consider the following code:
#include <type_traits>
enum class foo_bar : unsigned
{
foo,
bar,
};
int main()
{
foo_bar bar = foo_bar::bar;
// unsigned a = bar; -- error: cannot convert ‘foo_bar’ to ‘unsigned int’ in initialization
unsigned a = static_cast<std::underlying_type<foo_bar>::type>(bar);
return 0;
}
Why the implicit cast is not allowed? The underlying type is known, type of a
matches the foo_bar
underlying type. The implicit cast seems to be safe and could be performed without loosing an information. Why the cast is necessary from the language design perspective?
Aucun commentaire:
Enregistrer un commentaire