Author |
Topic |
|
Uflex
New Member
United Kingdom
8 Posts |
Posted - Jul 11 2017 : 1:59:23 PM
|
Add missing case statements doesn't work with auto and/or when the variable is a reference.
enum MyEnum
{
Value1,
Value2,
Value3
};
MyEnum var1;
MyEnum &var2;
auto var3 = Value1;
switch (var) When using var2 and var3 inside the switch and selecting the "Add Missing Case Statements", a warning dialog pops up saying "The switch type isn't recognized as an enum.". Using var1 works as expected.
It is easy to workaround but it would be a nice to have feature, especially as the const auto &var = [...] format is used a lot (well we do at the company I work for anyway).
Thank you! :) |
|
feline
Whole Tomato Software
United Kingdom
19021 Posts |
Posted - Jul 11 2017 : 3:19:23 PM
|
Interesting, an auto variable works just fine for me, so long as the auto is pointing at an enum type, and not an enum item :) I am seeing the problem with the reference to an enum type though, which is an interesting problem:
case=109635
What is the const auto line supposed to mean, and do? I don't recognise this syntax, so I tried adding the following line to a cpp file:
const auto &varOddThing = [oddItemOne, oddItemTwo, oddItemThree];
and this fails to compile, even in VS2017. |
zen is the art of being at one with the two'ness |
|
|
Uflex
New Member
United Kingdom
8 Posts |
Posted - Jul 12 2017 : 12:02:55 PM
|
Ah, auto indeed works in the example above if I type auto var3 = MyEnum; However, this is not valid C++. My example above (auto var3 = Value1;) was the correct way of initializing the auto variable.
Enum classes work the same way so in the example below, var4 compiles but doesn't allow adding missing case statements and var5 is the other way around: adding missing case statements works but this is not valid C++ so won't compile.
enum class MyClassEnum
{
Value1,
Value2,
Value3
};
auto var4 = MyClassEnum::Value1; // doesn't work with switch > add missing case statements
auto var5 = MyClassEnum; // add missing case statements works but this won't compile
Sorry about the const auto &var, I wasn't very clear. Anything after the = sign should be ignored and replaced with an rvalue. For example:
const auto &var6 = MyClassEnum::Value1; // var6 is of type MyClassEnum and contains the value Value1
const auto &var7 = Value1; // var7 is of type MyEnum (defined in my previous post) and contains the value Value1 Let me know if I still wasn't clear and can provide better examples. |
|
|
feline
Whole Tomato Software
United Kingdom
19021 Posts |
Posted - Jul 12 2017 : 12:19:37 PM
|
Perhaps I am missing something obvious here, but why would you have a switch statement on a type of enum item? Surely once the type is set to a specific enum item, the switch is redundant, since you already know the enum item it must be.
Or do I just not understand what is going on here? |
zen is the art of being at one with the two'ness |
|
|
Uflex
New Member
United Kingdom
8 Posts |
Posted - Jul 12 2017 : 12:34:23 PM
|
You're right, I have over simplified the example to have a minimal code that compiles. A more concrete examples would be (still very simple and without much purpose but you get the point):
enum class Type
{
Triangle,
Rectangle,
Pentagon,
// etc
}
Type GetShapeType(int sides)
{
if (sides == 3)
return Type::Triangle;
if (sides == 4)
return Type::Rectangle;
// etc.
}
const auto &shapeType = GetShapeType(4);
switch (shapeType) // add missing case statements won't work here
{
} |
|
|
feline
Whole Tomato Software
United Kingdom
19021 Posts |
Posted - Jul 12 2017 : 12:52:28 PM
|
This code makes much more sense, but the auto reference "shapeType" is still only a reference to the enum class, not to an actual enum item. I am concluding this since the return type of the function "GetShapeType" is the enum class, and super alt-g on the auto "shapeType" to go to the type takes me to the enum class, not one of the enum items.
True, the function is returning an actual enum item, but that's what happens when assigning a value to a variable of an enum type. The holding variable is still the type of the enum, and will accept any of the enum values, thus making a switch statement sensible.
Or am I missing or not understanding something here?
So as far as I can see this is the bug report I have already put in about refactoring failing when the variable is a reference, but with a different code sample, which I have now added to the bug report. |
zen is the art of being at one with the two'ness |
|
|
Uflex
New Member
United Kingdom
8 Posts |
Posted - Jul 12 2017 : 1:02:53 PM
|
We are on the same page yes about what you said, however auto does not seem to work in all cases. Here is some actual code:
// switching on actionData variable will allow VA to add missing cases statements
const RenderBitDepth actionData = static_cast<RenderBitDepth>(action->data().value<int32_t>());
// switching on actionData2 variable will generate the warning dialog when trying to add missing case statements
const auto actionData2 = static_cast<RenderBitDepth>(action->data().value<int32_t>()); Thank you very much for your patience :) |
|
|
feline
Whole Tomato Software
United Kingdom
19021 Posts |
Posted - Jul 13 2017 : 1:35:53 PM
|
I am glad we are on the same page about auto and enum type, I was starting to wonder myself somewhere along the line there
That's interesting, and I can see how we overlooked an auto created via a static_cast when testing this new feature. I have put in a bug report for this:
case=109692 |
zen is the art of being at one with the two'ness |
|
|
sean
Whole Tomato Software
USA
2817 Posts |
Posted - Dec 18 2017 : 7:23:46 PM
|
case=109635 is fixed in build 2248. |
|
|
|
Topic |
|