I just discovered that gcc and clang++, would let me use a const int for the underlying type of an enum. I wonder if that has any utility of if for all purposes it is the same as having an enum based on int.
I thought that may be it would make the enum instance not assignable, but it was not the case. (And to be honest, I thought it would not compile in the same way that you can't make a class derived from a const base type class C2 : const C1{})
**Is there any use or subtle difference between enum : int and enum : const int? ** If no, why would the compiler allow it?
Example:
#include<iostream>
enum A : const int{ // is this the same as enum A : int ?
no = 0,
si
};
int main(){
A a;
a = si; // is asignable
a = no; // twice
std::cout << (int)a << std::endl; // prints '0'
return 0;
}
Funny that I can do this enum A : volatile int as well. (Fortunately, I can't do this enum A : int& or enum A : int*.)
Aucun commentaire:
Enregistrer un commentaire