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 (1)

  1. First of all, there is a question of syntax, which ironically could be solved by taking a page out of BCPL's book (the irony is that BCPL is C's ancestor, but C lost several of its more advanced features, like statement expressions):

        double m[1024 * 1024] = ?;
        Foo x = ?;
    
    Second, there is a question of semantics. You only get to have 1 destructor, and it's called unconditionally — so we force the author of Foo (or of any class, really) to either 1) write the destructor that can properly destruct an unitialized object which is a bit tricky since reading any of the fields of such an object is UB; 2) shift the blame to the users: "don't do that with this class" — which completely negates your "decided at the call site (not in the class declaration)" point, because this decision is made in the class declaration, and can be made only there, not at the call site.

    So as usual with C++, we end up with a half-working system, with subtle footguns, pervading all of the program code, and impossible to ignore.