• lysdexic@programming.devOPM
      link
      fedilink
      English
      arrow-up
      0
      ·
      1 year ago

      Should focus on getting rid of undefined behavior.

      What problem do you believe is presented by undefined behavior?

      • mrkite@programming.dev
        link
        fedilink
        arrow-up
        1
        ·
        1 year ago

        It violates the principle of least surprise. You don’t expect the compiler to delete your bounds checking etc.

        The way c and c++ define and use UB is like finding an error at compile time and instead of reporting it, the compiler decides to exploit it.

  • MadhuGururajan@programming.dev
    link
    fedilink
    arrow-up
    0
    ·
    edit-2
    1 year ago

    I am trying C++ for this year’s advent of code. The most asinine thing i encountered is that the bracket operator on std::map writes 0 value if the key is not found. So your code doesn’t compile if you declare a const map or a const map reference as a function parameter. Compiler fails with “discards const qualifier on argument”.

    I mean, wtf?

    Edit: this is probably true for other STL containers

    • lysdexic@programming.devOPM
      link
      fedilink
      English
      arrow-up
      0
      ·
      1 year ago

      The most asinine thing i encountered is that the bracket operator on std::map writes 0 value if the key is not found.

      That’s a “you’re using it wrong” problem. The operator[] is designed to "Returns a reference to the value that is mapped to a key equivalent to key, performing an insertion if such key does not already exist. "

      The “0 value” just so happens to be the result you get from a default initializer whose default initialization corresponds to zero-initialization.

      If you want to use a std::map to access the element associated with a key, you need to either use at and handle an exception if no such key exists, or use find.

      • MadhuGururajan@programming.dev
        link
        fedilink
        arrow-up
        1
        ·
        edit-2
        1 year ago

        My point is that the design is wrong. Nobody expects [] as lvalue to update a value. Your argument is descriptive, mine is prespcriptive. I am saying that the C++ committee is wrong on this one (or whoever designed it this way)