C++26: The Oxford Variadic Comma

(sandordargo.com)

78 points | by ingve 4 days ago

9 comments

  • staplung 3 hours ago
    Of course since the old syntax is merely deprecated and not removed, going forward you now have to know the old, bad form and the new, good form in order to read code. Backwards compatibility is a strength but also a one-way complexity ratchet.

    At least they managed to kill `auto_ptr`.

    • Karliss 36 minutes ago
      I doubt it will be a problem in practice.

      Regular variadic arguments in general aren't used very often in C++ with exception of printf like functions. Not rare enough for majority of C++ programmers to not know about them, but definitely much more rare than their use in python. Main reason people know about it at all is printf. The "new" C compatible form has been supported since the first ISO standardized version of c++ if not longer. There haven't been a good reason to use the "old" form for a very long time. Which means that the amount of C++ code using deprecated form is very low.

      Being deprecated means that most compilers and linters will likely add a warning/code fix suggestion. So any maintained project which was accidentally using C incompatible form will quickly fix it. No good reason not to.

      As for the projects which for some reason are targeting ancient pre ISO standard c++ version they wouldn't have upgraded to newer standard anyway. So if new standard removed old form completely it wouldn't have helped with those projects.

      So no you don't need to know the old form to read C++ code. And in the very unlikely case you encounter it, the way for accessing variadic arguments is the same for both forms through special va_list/va_arg calls. So if you only know the "new" form you should have a pretty good idea of whats going on there. You might lookup in references what's the deal with missing coma, but other than that it shouldn't be a major problem for reading code. This is hardly going to be the biggest obstacle when dealing with code bases that old.

    • vlovich123 43 minutes ago
      I think Rust has shown a way to remove deprecated interfaces while retaining back compat - automated tooling to migrate to the next version and give a few versions for a deprecated interfaces to stick around at the source level.
    • hrmtst93837 1 hour ago
      If two template spellings trip you up, C++ is not your biggest problem. The joke is that each 'cleanup' sands off a tiny rough edge while the commitee leaves the old strata in place, so the language keeps accreting aliases and exceptions instead of dropping dead weight.
      • tialaramex 58 minutes ago
        Several times now C++ enthusiasts and indeed the committee have been told the way forward is the "Subset of a superset" that is, Step 1. Add a few new things to C++ and then Step 2. Remove old things to make the smaller, better language they want.

        Once they've obtained permission to do Step 1 they can add whatever they want, and in a few years for them it's time to repeat "Subset of a superset" again and get permission for Step 1 again. There is no Step 2, it's embarrassing that this keeps working.

    • Conscat 2 hours ago
      PyCuda 2024, used fairly often in certain industries, still contains `auto_ptr` ;-;
  • mFixman 3 hours ago
    I used to slay with this in code golfing competitions from TopCoder, where you had to implement a function to solve a particular problem, thanks to C pointer maths and the gcc generally putting function arguments in order in the stack.

    Turns out, these two are equivalent in practice (but UB in the C++ standard):

        double solve(double a, double b, double c, double d) {
          return a + b + c + d;
        }
    
        double solve(double a ...) {
          return a + 1[&a] + 2[&a] + 3[&a];
        }
    • mananaysiempre 2 hours ago
      > Turns out, these two are equivalent in practice

      Not in the x86-64 SysV ABI they aren’t. The arguments will be passed in registers (yes, even the variadic ones), so how your compiler will interpret 1[&a] is anybody’s guess. (For me, x86_64-unknown-linux-gnu-g++ -O2 yields, essentially, return a+a+a+a; which is certainly an interpretation. I’m also getting strange results from i686-unknown-linux-gnu-g++ -O2, but my x87 assembly is rusty enough that I don’t really get what’s going on there.)

    • camel-cdr 3 hours ago
      K&R syntax is -1 char, if you are in C:

          double solve(double a,double b,double c,double d){return a+b+c+d;}
          double solve(double a...){return a+1[&a]+2[&a]+3[&a];}
          double solve(a,b,c,d)double a,c,b,d;{return a+b+c+d;}
    • Joker_vD 1 hour ago

          double solve(double a[]) {
            return 0[a] + 1[a] + 2[a] + 3[a];
          }
      
          solve((double[]){1, 2, 3, 4});
      
      The cast in the invocation can be macro-ed away. And the best thing is, the actual stack layout and data movement/shuffling is pretty much identical to the approach with <stdargs.h>, and with no UB or compiler intrinsics.
  • advael 3 hours ago
    This seems pretty good to me just on the level of trying to read C as someone using C++. Parameter packs and variadic templates are easily the most confusing syntax in C++ and cleaning it up is... very welcome
    • jkaplowitz 3 hours ago
      Hats off for using "..." in your comment immediately after a valid identifier word and with no comma in between, given the topic of the article.
      • advael 2 hours ago
        Hats off for noticing. Not to be taken for granted in an increasingly skimming-oriented world
        • PaulDavisThe1st 35 minutes ago
          There remains the problem of defining what the meaning of ...is, is.
  • rusakov-field 1 hour ago
    Clarity and Elegance of Syntax > Backwards compatibility.

    In my opinion anyway. C++ feels so bloated these days.

  • zlfn 3 hours ago
    C++ seems to be constantly getting complicated. If the major version were to change, there wouldn't be any need for backward compatibility with the existing code, and it would have been okay to delete that syntax while creating an automatic formatter.
    • PaulDavisThe1st 33 minutes ago
      >If the major version were to change, there wouldn't be any need for backward compatibility with the existing code,

      I have no idea where you get this idea from. I expect gcc v28 to be able to compile C++ from 2008, and I'm not alone in that.

    • dnmc 3 hours ago
      Are you suggesting we move to C++++?
      • zlfn 2 hours ago
        It's already there. It's called C#
        • HackerThemAll 2 hours ago
          Well, C# also has its quirks already. Like the crippled finalizers which are never to be used. If the IDisposable interface had been correctly designed, finalizers could become be the "public void Dispose(void)". Or the manual passing of Task in case of async methods, which is... kinda smelly.
          • tialaramex 1 hour ago
            It's possible you didn't realise, but C# is sometimes said to be named that way because # is the symbol you get if you draw ++ small and then on the line below ++ again. Hence C++++

            All languages have some spikier edges, there are no languages I know where I think "Well, even if we could start over I have no idea how to improve this". What's notable about C++ is just how rich that "could do better" seam is, so very many of their defaults are wrong, so many of their keywords are the wrong word, so many of their standard library features are allowed, sometimes even mandated to be crap.

            • kstrauser 1 hour ago
              I don’t know if that’s true or not, but while the thought never crossed my mind before your comment, it’s now canon in my mind. Yes, C# is spelled C++++ with a ligature.
            • HackerThemAll 1 hour ago
              I think you meant to get that to the original poster, who seems to imply C# is the flawless, bestest incarnation of C\+\+(\+\+)+.
      • m-schuetz 2 hours ago
        Personally I like C+. Picking the nice parts of C++, but skipping all the nonsense. I just wish C++ hadn't deliberately screwed up designated initializers with mandatory ordering. The C version of it that allows out-of-order assignments is clearly superior.
      • dist-epoch 1 hour ago
        Didn't you read the article? That form is deprecated. The recommended one is C,++,++
        • PaulDavisThe1st 32 minutes ago
          You clearly didn't read it either. Going forward, it will be C++...
  • blueaquilae 1 hour ago
    I'm far from C++ but reading this article confused be, from the form to the impact to the dead link https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p12...

    I guess that's a preview how C++ require a lifelong commitment.

  • throwaway2027 3 hours ago
    C++ got too complicated after C++23 I went back to C.
    • FartyMcFarter 2 hours ago
      You can always restrict yourself to a subset of C++ that takes advantage of RAII (resource handling is extremely painful in C), and get performance benefits like move semantics, without the insanely complex stuff.

      I love C, but C++ has worthwhile advantages even if you heavily restrict which features you use.

    • Conscat 2 hours ago
      No offense intended to your perspective, but I do find it a little amusing that C++23, which was generally considered a disappointingly small update due to COVID complications, was the breaking point in complexity.
    • jjgreen 2 hours ago
      C is the new vinyl.
  • lasgawe 3 hours ago
    learned something new. thanks for the article.
  • HackerThemAll 2 hours ago
    Yes, but no. I learned C++ in '90s when it was C with classes and some other noise added by Stroustrup. During the some 25 years that followed it had became a mess that's insanely hard to work with. I'm not going back to this language. I prefer plain C or Rust, leaning towards Rust when I fully comprehend the lifetime and borrow checker. Or when I have the luxury of having a GCed runtime, then the .NET with its easiest C# language with wonderful abundance of great libraries is the best choice. Nobody was ever fired for using .NET (for right purposes).
    • PaulDavisThe1st 27 minutes ago
      Tiring how often this needs to be said, but if you want "C with classes", you can just use C++ that way.

      I've been using C++ for more than 30 years (I added thread_local to Cfront back in the early 90s), and while the language has grown dramatically in that time, there is fundamentally nothing that would prevent me from writing "C with classes" using the modern version.

      I don't do that because I also like RAII, and polymorphism, and operator overloading and ...

      I've never used .NET and could not imagine any scenario under which I would. The libraries that matter to me are mostly written in C or C++ and there are more of them than I'd ever need, mostly.

      • QuadmasterXLII 9 minutes ago
        Languages are both read and written, restrictions like OP is pining for are fundamentally for reading. As such, it is not terribly helpful that they can opt in to restrictions when writing.