C++ Weekly Ep 467 - enum struct vs enum class

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

Upcoming Workshops:

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

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

I didnt know that enum struct exists.... O_O

xvoidee
Автор

Logically, I agree with you, struct = public. The problem comes with junior and less-experienced folks looking at the code later - if they're not familiar with the concept, "enum class" is a lot more popular as a search term.

reverendragnarok
Автор

I can fully see the point of using `struct` simply from a semantic perspective, as you describe. The reason I'd otherwise use `class` is simply because it is most commonly used in code bases I've seen out there.

gustafbstrom
Автор

enum struct was actually surprising to me. Any idea what's the rational of introducing 2 different syntaxes with the same semantics?

TsvetanDimitrov
Автор

Yep, like others say, definitely should *not* be enum struct, because it's "weird", no reason to do it, not searchable, confusing to other readers. Not sure why they allow "enum struct" and "enum class" but then *don't* have public/private or member functions (enums sorely need them).

snbvreal
Автор

enum class is less letters that enum struct. this reminds of what's the difference between template<typenname T> template<class C> and template<struct S>

TheBrainDunne
Автор

Stroustrup never mentions "enum struct" in his book, and quite possibly the reason "enum class" is preferred. I'd say that just because you can do something doesn't mean you should. For the sake of consistency, I'd argue with using "enum class". The only reason for using "enum struct" is "because you can".

paulej
Автор

Since I never even heard of "enum struct" I used the "class" version. I think the "struct" version will stay more exotic even if it is more logical just like east const.

LiveseyMD
Автор

I'd argue that "enum struct" already refers to a C enum defined in a struct, therefore we use enum class to refer to typesafe enums.

AxWarhawk
Автор

Why did the standard committee make both of these available?

AGeekTragedy
Автор

I tend to use c-style 'struct' for all the classes that don't have any private members and member functions, and are in general small or compact. And 'class' for anything that needs more refinement. In that regard I lean towards enum struct.

krishawkeye
Автор

For a moment i thought you were going to do bitwise operations without need for casting. I was happy.

zahell
Автор

I prefer enum class because it has the same feel as a class over a struct to me. Ignoring the technical details I read structs as implying a looser definition where internals are free and easily accessed and mixed, while classes imply more controlled access to the internals. Likewise enum class is a stricter enum that can't easy be mixed with other values without casting.

DaphnePfister
Автор

I use enum class because that's slightly shorter, many people don't even know enum struct is valid and it doesn't matter anyway because public/private don't apply here. Also, I believe making this enum class/enum struct was a mistake. This should have been enum namespace because namespaces don't have public/private either but they do define a scope.

gnolex
Автор

I would consider this is a distinction without a difference- the name does not imply the semantics. It's akin to saying `template <class T> void foo(T t)` can only accept types declared as 'class'.

timhaines
Автор

I had not realized you could use `enum struct` instead of `enum class`, but I do buy your argument that `enum struct` kind of makes more sense. I'm also more often declaring "classes` using `struct` rather than class, because it makes more sense to have members being public by default rather than private. Though professionally I'm using `class`, as most code bases I'm working with uses `struct` to to indicate that objects are intended to be C-like structs

EmilOhlsson
Автор

I am a lazy developer. I like to type as few keywords as possible. In one of my code bases I tried doing _everything_ as struct. Only only mark those things private where necessary. And it works wonders. Less keyword clutter. Correct defaults for constructors (which are at the top). And it becomes very apparent when the constructors are not meant to be called because it clearly say 'private'.

Same applies to inheritance. Only the odd occassion of private inheritance should be clearly visible. Thus, no need to write public there either.

daantimmer
Автор

Since the recent addition of `using` declarations for enums, I would suggest to use `enum struct` for those where the using declaration is expected [i.e. a near replacemnte of C enums] and leave `enum class` for the rest...

MarcoFantozzi-wp
Автор

It would have been a useful distinction to make `enum struct` convertible to `int` but not `enum class` (not even with static_cast), making the internal int values completely private. I think it was a missed opportunity. With the current language, if one wants to have an enum in which the integer values are completely private, one has to simulate it with a complicated class.

AlfredoCorrea
Автор

In the MSVC ABI, struct and classes actually result in different linkage. Is the same true for enum struct vs enum class? If it is, it might cause some headaches to be inconsistent about it when forward declaring an enum elsewhere to avoid including its whole header file.

Minty_Meeo
welcome to shbcf.ru