• Why no string switch? (was: Resources to learn common lisp?)

    From ram@ram@zedat.fu-berlin.de (Stefan Ram) to comp.lang.misc,comp.lang.c++ on Sat Sep 19 12:46:17 2026
    From Newsgroup: comp.lang.c++

    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.

    Newsgroups: comp.lang.misc,comp.lang.c++
    Followup-To: comp.lang.c++


    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From boltar@boltar@caprica.universe to comp.lang.misc,comp.lang.c++ on Sat Sep 19 14:47:27 2026
    From Newsgroup: comp.lang.c++

    On 19 Sep 2026 12:46:17 GMT
    ram@zedat.fu-berlin.de (Stefan Ram) gabbled:
    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.

    And? for() used to just be a simple comparison check loop then ranged for
    loops were introduced. Functions had to be defined inline then lambdas were introduced. Whats your point? There's no reason switch() can't be internally overloaded so a POD switch continues to be compiled down to a simple jump table
    whilst more complicated comparisons generate an if-else tree.

    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.

    That might be the case if the last 15 years of C++ development didn't prove otherwise.

    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Keith Thompson@Keith.S.Thompson+u@gmail.com to comp.lang.c++ on Sat Sep 19 15:42:32 2026
    From Newsgroup: comp.lang.c++

    ram@zedat.fu-berlin.de (Stefan Ram) writes:
    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.

    std::cout << x << '\n';

    This looks like it's just using core language features, but it can
    result in arbitrarily complex actions because operator<< is overloaded.
    There are plenty of other examples.

    The fact that C++ doesn't currently have switch on strings or equivalent doesn't greatly bother me, but if such a feature were added it would be 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.)
    --
    Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
    void Void(void) { Void(); } /* The recursive call of the void */
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From boltar@boltar@caprica.universe to comp.lang.c++ on Sun Sep 20 07:36:46 2026
    From Newsgroup: comp.lang.c++

    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.

    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From David Brown@david.brown@hesbynett.no to comp.lang.c++ on Sun Sep 20 10:06:58 2026
    From Newsgroup: comp.lang.c++

    On 20/09/2026 09:36, boltar@caprica.universe wrote:
    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.


    Yes.

    It is conceivable that a compiler could take advantage of additional
    features of the class, such as order operators or constexpr hashes, in
    order to generate more efficient match/switch code. (Just as they may generate binary trees or tables for today's switches.) But that would
    be transparent to the user - a "==" operator is the base.


    --- Synchronet 3.22a-Linux NewsLink 1.2