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. -> EDIT it does, intellisense is wrong
// 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 aswell.
enum class TypeClass
{
A, B, C
};
int main(int argc, char** argv)
{
std::optional<std::vector<TypeClass>> types;
types = { TypeClass::A };
types = { TypeClass::C };
}
A == 0andC == 2. Your assignment creates a vector of those sizes.types = { 2 }not compile?types = { A };andtypes = { C };constructs / assigns astd::vector<Type>containing one element. What compiler makes the program show the sizes0and2?1and1and the reason7doesn't work is that's7is not aType. You could force it though:types = { Type(7) };. Can you upgrade to MSVC2022? It should hopefully be better.