0

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 };
}
11
  • Because A == 0 and C == 2. Your assignment creates a vector of those sizes. Commented Jun 3, 2024 at 15:12
  • @Someprogrammerdude yes thats the conclusion I wrote down, but then why does types = { 2 } not compile? Commented Jun 3, 2024 at 15:13
  • 3
    Hmm... both types = { A }; and types = { C }; constructs / assigns a std::vector<Type> containing one element. What compiler makes the program show the sizes 0 and 2 ? Commented Jun 3, 2024 at 15:14
  • 1
    @TedLyngmo This is on msvc2019 Commented Jun 3, 2024 at 15:17
  • 2
    @Aedoro I'd say MSVC2019 is buggy in that case. It should print 1 and 1 and the reason 7 doesn't work is that's 7 is not a Type. You could force it though: types = { Type(7) };. Can you upgrade to MSVC2022? It should hopefully be better. Commented Jun 3, 2024 at 15:18

1 Answer 1

1

This seems to be another msvc bug which is fixed in the latest version. Latest version of msvc start accepting the program just like gcc and clang does. Demo

Sign up to request clarification or add additional context in comments.

1 Comment

That's a long-time-running bug in Microsoft compiler related to coversions in intializer (another symptom - failing to compile if a unified initializer contains float instead of double or int instead of int64) e.g. int sz = 42; std::vector v = {sz}; fails, And it STILL not fully fixed in recent versions. Most of the time such code can be avoided. It almost looks like it gets flagged as "it matches our code standard - won't fix it", which was known to be a thing in past.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.