C++ Weekly - Ep 232 - C++20's `using enum`

preview_player
Показать описание
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟

T-SHIRTS AVAILABLE!

WANT MORE JASON?

SUPPORT THE CHANNEL

GET INVOLVED

JASON'S BOOKS

► C++23 Best Practices

► C++ Best Practices

JASON'S PUZZLE BOOKS

► Object Lifetime Puzzlers Book 1

► Object Lifetime Puzzlers Book 2

► Object Lifetime Puzzlers Book 3

► Copy and Reference Puzzlers Book 1

► Copy and Reference Puzzlers Book 2

► Copy and Reference Puzzlers Book 3

► OpCode Puzzlers Book 1


RECOMMENDED BOOKS

AWESOME PROJECTS

O'Reilly VIDEOS

Рекомендации по теме
Комментарии
Автор

The one feature I've been waiting for is the automatic compile time creation of strings for enum values so you don't need to write switch statements like that anymore.

AusSkiller
Автор

As days go by it's becoming more and more of a pleasure to program in c++

DamianReloaded
Автор

That is useful for templated wrapper classes where you want add some functionality to a group of enums and still be able to access to the elements directly like :
Mywrapper<Myenum>::value1

ashtum
Автор

FWIW, it's quite common for VS previews to offer compiler support and then have the Intellisense support come in the actual release.

Qazqi
Автор

Even though I have been programming for over 40 years, I seem to be too busy to learn new features, in this case in C++20. Thanx for great videos about this subject!

Lexitivium
Автор

I recently used this feature without even realizing it was only in c++20 and I used it in a way where depending on the platform it is compiled for it switches the "using enum" declaration out for a different enum with the same names and different values for keymapping in a single function

christopheriman
Автор

For a moment I hoped that we finally had a quick way to stringify enum, but I guess this is okay too...

surrog
Автор

Hey, did you consider putting a link to the code in video description?
Sometimes the code you write is relatively long / complex.
I'd love to have a link to Godbolt, where I can get right to experimenting with it!
(well, this time it's not in Godbolt and it's really simple, but usually it's not)

Btw, thanks for all the work you're doing, I love this series.

Kaznovx
Автор

I think you can put the using declaration inside the switch statement - before the first case.

simonhrabec
Автор

if your enum class/struct is inside multiple namespaces you could shorten it with decltype(v)::Value1 and decltype(v)::Value2

SeppyYT
Автор

Another awesome video! Love all of your content.

KhalilEstell
Автор

Great video and super informative as always

Since enumerations are a subject that I have very strong and seemingly uncommon opinion on, it would be interesting to hear your take on it(and other commenters).
I heavily dislike and actively avoid enum class, because to me it magnifies and "exposes" the most dangerous uses of enum, where it lets you somewhat safely use an enum as something other than another name for a specific integer. Sure, enum class makes this type safe, but really all we have done then is turned the enumeration concept into a more complex and slightly less verbose const static int, while allowing for confusing use-cases that are often counter-productive to the clarity which enums in my view are supposed to provide.

Whereas the implicitly cast and "weak" version of enumeration that normal enum represents are essentially a way to simply rename integers through find and replace, something incredibly useful for readability and a feature that simply isnt available to us through any other feature. The fact that it is impossible to stringify an enum without a ton of work is a feature and not a bug. You dont use a hammer to wash your dishes either. If name-collisions(or whatever the technical term is) are a problem, simply wrap the enum in a namespace.

TL;DR
I'm mad that I can't get implicit cast enumerations with no runtime overhead in C#. Enumerations are supposed to be find and replace integers, not smart and typesafe objects. Stop trying to get them to be something they're not.

Have a nice day!

RejoiceXreload
Автор

I'm looking at doing a to_string for an enum class with hundreds of values. My plan at the moment is to initialize a std::map with the pairs of value/string. Then probably wrap that to do what I want in the case of incorrect/undefined values etc.
Would a massive switch or something else be better?
By the way this is a cool trick, but I think I'm stuck at c++17 on this particular project for the time being.

s
Автор

Aw man. I just want scoped enums without strict typing.

Spirrwell
Автор

I can't use it with template in a class :(

RickoCraft
Автор

A bit weird for me that they made a feature to unscope a scoped enum.

platin
Автор

Edit4: Arrrh, - forget the following text, I got it now - look at my own reply to this post. :-)

No easy way to increase the font size?!!! You've already used it previously I can see, - Look at the dropdown in the lower left corner of the editor pane saying "214%" :-)
Edit1: PS. You can set your own "%" by typing the desired value if you don't want the default selected drop-down values.
Edit2: And ctrl+mouse-wheel also works.
Edit3: ctrl+shift+COMMA/DOT zoom in/out on my system (Check out (type it in edit box "Show commands containing:"))

siggimund
Автор

Compiler Explorer offers MSVC v19.24, which supports using enum with /std:c++latest.

georgetokmaji
Автор

How do you activate the c++ 20 compiler on Visual Studios?

KingKarEl
Автор

Would it be better to store the strings in a constexpr array, static_cast the enum to an int index, and return .at(index)?

keithmn