• Re: Why no string switch?

    From Keith Thompson@3:633/10 to All on Sat Sep 19 15:42:32 2026
    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 */

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From boltar@3:633/10 to All on Sun Sep 20 07:36:46 2026
    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.


    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From David Brown@3:633/10 to All on Sun Sep 20 10:06:58 2026
    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.



    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)