inspect (mystr) {
"hello" => { /* ... */ },
ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:
inspect (mystr) {
"hello" => { /* ... */ },
Why there is no string "switch" statement in C++
C++ is a systems programming language with zero-cost abstraction.
The core language consists of predictable, low-level primitives
(like "if", "while", "switch", or "+") that map to basic machine
instructions and require minimal runtime overhead.
If a native C++ inspect statement allowed matching a string variable
against string cases, a single "primitive-looking" statement would
actually execute an O(N) string-comparison loop at runtime. Depending
on how the compiler tries to optimize it, it might generate a sequen-
tial chain of string comparisons, a trie structure, or a hashing rou-
tine.
Introducing a feature into the core language grammar that can intro-
spect, match, and switch on complex string types blurs the historical
boundary between lightweight primitive instructions in the core lan-
guage and complex library types, making it more difficult for program-
mers to "see" the run-time costs of core language features.
ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:
inspect (mystr) {
"hello" => { /* ... */ },
Why there is no string "switch" statement in C++
C++ is a systems programming language with zero-cost abstraction.
The core language consists of predictable, low-level primitives
(like "if", "while", "switch", or "+") that map to basic machine
instructions and require minimal runtime overhead.
If a native C++ inspect statement allowed matching a string variable
against string cases, a single "primitive-looking" statement would
actually execute an O(N) string-comparison loop at runtime. Depending
on how the compiler tries to optimize it, it might generate a sequen-
tial chain of string comparisons, a trie structure, or a hashing rou-
tine.
Introducing a feature into the core language grammar that can intro-
spect, match, and switch on complex string types blurs the historical boundary between lightweight primitive instructions in the core lan-
guage and complex library types, making it more difficult for program-
mers to "see" the run-time costs of core language features.
consistent with a lot of other high-level C++ features that look like
core language syntax. (And presumably it would be designed to work
on arbitrary types, not just strings.)
On Sat, 19 Sep 2026 15:42:32 -0700
Keith Thompson <Keith.S.Thompson+u@gmail.com> gabbled:
consistent with a lot of other high-level C++ features that look like
core language syntax. (And presumably it would be designed to work
on arbitrary types, not just strings.)
Indeed. As I said in an earlier post the object class being switched on would simply need to implement operator==() else the compiler would error.
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 74 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 121:05:45 |
| Calls: | 1,194 |
| Files: | 1,352 |
| Messages: | 290,204 |