Bobinas P4G
  • Login
  • Public

    • Public
    • Groups
    • Popular
    • People

Conversation

Notices

  1. Bernie (codewiz@mstdn.io)'s status on Saturday, 19-Mar-2022 17:47:58 UTC Bernie Bernie

    Old C++ libraries suchas Microsoft's MFC followed the misguided "double construction" approach, where constructors must initialize the object to an empty state, and then you have to call Create() or Init() to actually make the object usable.

    There are unfortunately many #Cpp developers who still believe this is a good idea. The main motivation is that constructors cannot return an error.

    I argued that exceptions are bad, but throwing is the only way to return an error from a constructor.

    In conversation Saturday, 19-Mar-2022 17:47:58 UTC from mstdn.io permalink
    • Bernie (codewiz@mstdn.io)'s status on Saturday, 19-Mar-2022 17:54:02 UTC Bernie Bernie
      in reply to

      I have no experience using exceptions, but I think the current best practice is to never throw from default constructors and move constructors.

      There's nothing in the language enforcing it... and it becomes hard to verify with templated code.

      C++20 concepts allow specifying these requirements explicitly, but I don't have much experience using concepts either.

      #cpp #programming

      In conversation Saturday, 19-Mar-2022 17:54:02 UTC permalink
    • Bernie (codewiz@mstdn.io)'s status on Saturday, 19-Mar-2022 17:59:02 UTC Bernie Bernie
      in reply to

      And then there's noexcept. Actually, there are two: the noexcept function specifier and the noexcept operator. The former replaced the old throw(), and the latter returns true if the given expression cannot throw (with some caveats).

      The talk I linked to another branch of this thread has all the details, if you can stand the speaker advertising his own book on every single slide:

      https://mstdn.io/@codewiz/107976529664463919

      #cpp #programming

      In conversation Saturday, 19-Mar-2022 17:59:02 UTC permalink
    • Jonas Hultén (bjonte@mastodon.technology)'s status on Saturday, 19-Mar-2022 21:02:52 UTC Jonas Hultén Jonas Hultén
      in reply to

      @codewiz I have over the years tried to understand how to best use exceptions and I have decided to only use it for exceptional cases where the program can’t continue and must be taken down. All else is handled by simple return value mechanisms. No library or generic functions should use it since it’s creates a hell of different types because of the lack of a strict standard. So it leaves the main program to use its own.

      In conversation Saturday, 19-Mar-2022 21:02:52 UTC permalink
    • Bernie (codewiz@mstdn.io)'s status on Saturday, 19-Mar-2022 21:02:52 UTC Bernie Bernie
      in reply to
      • Jonas Hultén

      @bjonte For that, there's the simpler abort() mechanism or std::terminate():
      https://en.cppreference.com/w/cpp/error/terminate

      Both invole the atexit() handlers, which can be used for emergency cleanups (restore the terminal to a functioning state, delete temporary files, dump some debug info...

      In conversation Saturday, 19-Mar-2022 21:02:52 UTC permalink

      Attachments


    • Bernie (codewiz@mstdn.io)'s status on Saturday, 19-Mar-2022 21:14:53 UTC Bernie Bernie
      in reply to
      • Jonas Hultén

      @bjonte I am in favor of aborting programs whenever there's no meaningful way to recover from the error.

      For instance, trying to handle a NULL result from malloc() in a server is usually pointless: if the heap became full due to leaks or hung queries, you should just abort and leave it to systemd to start a fresh instance.

      Death is more honorable than lingering around while unable to serve queries.

      In conversation Saturday, 19-Mar-2022 21:14:53 UTC permalink

Feeds

  • Activity Streams
  • RSS 2.0
  • Atom
  • Help
  • About
  • FAQ
  • Privacy
  • Source
  • Version
  • Contact

Bobinas P4G is a social network. It runs on GNU social, version 2.0.1-beta0, available under the GNU Affero General Public License.

Creative Commons Attribution 3.0 All Bobinas P4G content and data are available under the Creative Commons Attribution 3.0 license.