0

I read the following in the "Make interfaces precisely and strongly typed" section of C++ Core Guidelines, I.4:

"For the case of a set of boolean values consider using a flags enum; a pattern that expresses a set of boolean values."

So it seems as if there is supposed to be a standard way to do this. For example, the idea of using scoped enums (in C++11 and up) sounds interesting. However, it doesn't seem like any implementation is really official, and every attempt gets some criticism. What is the standard here?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • 1
    `enum { A = 0x0001, B = 0x0002, C = 0x0004, ...} Flags`. No 'official implementation' required. – user207421 May 03 '23 at 01:31
  • Does this answer your question? [Are enums the canonical way to implement bit flags?](https://stackoverflow.com/questions/24251427/are-enums-the-canonical-way-to-implement-bit-flags) – Karl Knechtel May 03 '23 at 01:33
  • 1
    "Better off if such implementation doesn't use the STL (some C++ compilers doesn't even include it)." - that makes no sense; "STL" hasn't been a thing [since the mid-90s](http://www.stlport.org/doc/sgi_stl.html). It's sometimes used inaccurately as a nickname for the **C++ standard library**; I have no idea what compilers you've encountered that don't provide that - especially if they're pretending to implement C++11, or any other actual standard. – Karl Knechtel May 03 '23 at 01:38
  • Anyway, I removed that along with the rest of my editing. Please try to keep in mind that this is **not a discussion forum**; posts here should be **questions** that are **directly** about **the code, not** you, your personal perspective as an embedded developer, etc. – Karl Knechtel May 03 '23 at 01:39
  • I use `enum class` for the bit flags, and provide the operations (via operator overload) for the way I want the bit flags to allowed to be manipulated (usually `operator|` for combining and `operator&` for masking, and by my own convention unary `operator+` to convert to its underlying type). – Eljay May 03 '23 at 02:13
  • 2
    An enumeration is one common way to do things. The basic thing to keep in mind is that you want to give names that will be meaningful to a user. If you have something like `x = file(name, true);` it's pretty hard for anybody to guess what `true` means in this case. Changing that to an enumeration like `x = file(name, EnableSharing);` makes the meaning of the second parameter obvious. A `bool` can be meaningful, but something else is usually better, especially if there are two or more such parameters (e.g., consider `foo(true, false, true)` vs. `foo(true, true, false)`). – Jerry Coffin May 03 '23 at 02:30
  • But also note that scoped enumerations aren't a panacea either. Especially if you want to be able to combine them with bitwise `or`, (e.g., like `open_file(..., read | binary)` a scoped enumeration may cause more harm than good. It often makes sense to use a normal enumeration that's a public member of the class, so that would still be something like `file::read | file::binary)`. – Jerry Coffin May 03 '23 at 02:37
  • @user207421, my question is towards scoped enums implemented since C++11. It's a new data type that doesn't mix with integers (unless you casting them). – Francisco Rodríguez May 04 '23 at 04:17
  • @KarlKnechtel, It's not my personal view on ES, it's a fact: AVR compiler last release (from 2017) does implement C++17, however, STL isn't available, that's way I would like to know if there exist an official implementation out of the STL. – Francisco Rodríguez May 04 '23 at 04:23
  • @Eljay, The problem is when you want to discriminate individually your bit flags inside a switch statement, as shown here: https://stackoverflow.com/a/32579209/1496939 – Francisco Rodríguez May 04 '23 at 05:00
  • A `switch` statement is the wrong tool for the bitflags job. – Eljay May 04 '23 at 12:43
  • @FranciscoRodríguez what I am trying to tell you is that the phrasing "STL is/isn't available" doesn't make any sense in the first place. – Karl Knechtel May 05 '23 at 04:07

0 Answers0