In the following case, when using assignment on an optional vector of enum values I cannot explain the behavior of the code. Why does assignment of ... = {C}
compile and create a vector of size 2, while ... = {2}
does not compile?
enum Type
{
A, B, C
};
int main(int argc, char** argv)
{
std::optional<std::vector<Type>> types;
types = { A };
std::cout << types->size() << std::endl; // => 0
types = { C };
std::cout << types->size() << std::endl; // => 2
// Conclusion, this uses the vector size constructor as the enum can be converted to integer (sicne it is not defined as enum class).
// So i should be able to just provde the size as an integer instead of a `Type`? but:
// types = { 7 };
// does not compile.
// Then what is allowing the previous lines to use the vector size constructor?
}
And this works as expected, which gives the behavior I expected for the first example aswel.
enum class TypeClass
{
A, B, C
};
int main(int argc, char** argv)
{
std::optional<std::vector<TypeClass>> types;
types = { TypeClass::A };
types = { TypeClass::C };
}