Hacker News new | past | comments | ask | show | jobs | submit login

I have a strong dislike for the enhanced conditionals. Like, the feature itself is fine... but there's just a certain subset of people who think any new C++ feature is the blessed "proper" way to accomplish any task and this feature is very abusable. It also doesn't provide very much benefit to actual practice in return.



> I have a strong dislike for the enhanced conditionals.

So don't use them. C++ is a multi-paradigmatic language, you don't have to (and typically can't, really) put all features to use.

I'm not such a great fan of them either, but it's not like they would confuse me if I saw them in somebody else's code.

https://www.youtube.com/watch?v=cWSh4ZxAr7E&list=TLPQMjkwNTI...


For some additional context, this came up for me because some other team had snuck verbiage into our style guide that mandated things like this instead of traditional initializers:

    void foo() {
      if (auto [a, b, c] = std::make_tuple(x, y, z); true) { /* [...] */ } 
    }
Yeah you can still read this, but it's stupid to support constructs creating scope, doing control flow, and initializing arbitrarily many variables simultaneously (which may invoke constructors of their own). The relatively minor benefits to things like iterators are not outweighed by the burden of supporting this stupid code.


If the point is to limit the scope of specific variables, wouldn't it be simpler to write

    void foo() {
        {
            auto [a, b, c] = std::make_tuple(x, y, z);
            /* [...] */
        } 
    }


I think the original comment's point is exactly that. But people see the new expression type and want to apply it in this case.

I'm of two minds on this. I can see the impulse and why you'd reach for it: Block lexical scopes with no if/while/etc statement attached read a bit odd. Introducing an "unattached" block means as a reader/reviewer I want to know why the scope has been created. So in this new syntax I suppose makes it "clearer" (in some respects) that what is being done here is introducing a new lexical scope specifically for the given variables. Like I commented elsewhere, this is somewhat similar to the ML-languages "let <assignments> in <block>" syntax, which I have always found admirable, as it makes clear to the reader (and compiler) what scope and state are being dealt with.

On the other hand, this is so out of step with C/C++ style generally, and it seems so excessively "clever" that I think it's going to piss people off. And because it's bolted onto the conditional expr, you get the pointless ;true there.

Having a with ( ... ) syntax would have been nicer?

  with (auto [a, b, c] = std::make_tuple(x, y, z)) {
  }
I'm curious what Titus Winters and the Google C++ style guide is saying about this.


I'm actually not curious about what Google's guides say, and don't accept them as technical leaders.

Having said that: It's not really unlike C++, since you have it in for loops:

    for (int x = 0; x < n; x++) { do_stuff(); }
which is like

    {
        int x;
        for (x = 0; x < n; x++) { do_stuff(); }
    }

anyway, I wouldn't mind the syntactic sugar of "with X=Y do Z" or "let X=Y in Z"


Having the compiler deduce the return type of a function depending on the constexpr path taken is really useful.


Different feature, I'm talking about the optional initialization section added to conditionals. It allows moving declarations to the point of use like for statements. I've also met people who think it's now the appropriate place to declare all block scope variables, which is horrifying.


It's horrifying but maybe also intriguing?

Has shades of the OCaml/StandardML "let ... " scope:

StandardML:

  let
    val a = 1
    val b = 2
  in
    a + b
  end
It's nice for making it clear that the scope of the assignment is restricted to this lexical scope, though there are other ways to do this in C/C++.

I work in Rust these days so haven't had a chance to use much of this, but I wonder if this has some nice RAII type use patterns? I may now go explore.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: