An unofficial, read-only Hacker News reader. Join the beta →

In C++, = default and {} are not the same constructor (opens external site)

129 points by surprisetalk 2 days ago | lemire.me | 198 comments on HN

View full thread · View on Hacker News

Comment thread (6)

  1. I wholeheartedly disagree with the idea that all C++'s problems come from backwards compatibility with C. Even "pure" C++ alone without C constructs brings enough footguns.

      struct Config {
        std::vector<std::string> names;
        const std::vector<std::string>& getNames() const { return names; }
      };
      Config loadConfig();
      
      // which of these are safe?
      for (const auto& n : loadConfig().names)      { use(n); }  // A
      for (const auto& n : loadConfig().getNames()) { use(n); }  // B
      
      const auto& v1 = loadConfig().names;          // C
      for (const auto& n : v1) { use(n); }
      
      const auto& v2 = loadConfig().getNames();     // D
      for (const auto& n : v2) { use(n); }
    
    Trick question btw
    Replies (2)
    1. Why not simply write the following?

         const Config config { loadConfig() };
         for (auto &name : config.names) {
             use(name);
         }
      
      Or else you could have "loadConfig()" be a method that returns a reference, which is in turn the member of a class, so that there isn't any dangling reference issue.
      Replies (1)
      1. "You could write it completely differently." is just a more general case of "You could write it in Rust instead.". And it's missing the point.

        The point being made is that the stumbling blocks exist to be tripped over by the unsuspecting, and are not because of C.

        Replies (1)
        1. They admitted it was a trick question. I just don't think it would have been written as they wrote it by anyone who knew C++ to a decent level in the first place. Maybe that makes it a stumbling block as you're learning it, but it seems obvious to do the simplest thing first, at least in my view.

          (Also, this is at least in part inherited from C. Dangling pointers themselves are due to C. And temporary expressions are just acting consistently with C semantics here.)

    2. C in C++20, A, B and C in C++23. This does sort of fall under a backwards compatibility issue, albeit not with C, as making D valid was considered too much of a breaking change.
      Replies (1)
      1. I'm no C++ expert, but in P2012R0 it even gives this specific example, indicating that A is OK in c++20:

        [..] One recent real-world example by one of the authors is the following: Improving the following code:

          struct Person {
           std::vector<int> values;
           …
          };
          for (auto elem : createPerson().values) { // OK (lifetime extended) <-----------
        
        by introducing a getter to have better encapsulation, suddenly caused the undefined behavior:

          class Person {
           private:
           std::vector<int> values;
           …
           public:
           const auto& getValues() const {
           return values;
           }
          };
          for (auto elem : createPerson().getValues()) { // UB (fatal runtime error)