10 comments

[ 2.6 ms ] story [ 26.4 ms ] thread
That's a C99 feature, designated initializer. Hardly modern. Yes it was ported to C++ relatively late, but it happened in C++20.
If you're calling this across translation units, the calling convention will come with a performance penalty, but boy have we come full circle since pre-ANSI C required you to pass args as a struct. Much love - wish the language required struct and arg list to be the same thing. You can send a list of em and it'll work with algebraic data types for batching calls. The dream. CPU doesn't play nice though since structs aren't register shaped, but maybe they could be in a future calling convention.
To me, “keyword arguments” means actual language keywords being used as arguments, like “minute” or “hour” in T-SQL’s DATEDIFF, for example: `SELECT DATEDIFF( hour, NOW(), someDateCol )`.

…but I think the author meant “named arguments”, like we have in C#, Swift, and Objective-C.

Reminds me of an idea I had years ago, for implementing "named binary operator syntax" in C++ so that stuff like the following would work:

    int x = 5 <xor> 3; // x = 6
The basic trick was to notice that this is really parsed as:

    int x = ((5 < xor) > 3);
which you could implement with (roughly):

    struct XorType1 { ... } xor;

    struct XorType2 {
      int left;
      XorType2(int left) : left(left) {}
      int operator>(int right) const {
        return left ^ right;
      }
    };

    XorType2 operator<(int left, XorType1 const& ignored) {
      return XorType2(left);
    }
But I sat on this for a while and later discovered someone else had already come up with it :-/

EDIT: Thanks commenter hawkice for fixing my XOR arithmetic!

I pretty much always prefer using an options struct as soon as there is more than one optional argument.

Comes out cleaner because overriding a default argument doesn’t force you to also do all the positional arguments in front of it.

Designated initializers make it look really nice imo. I feel like the brackets are no big deal.

Python has sort of the opposite when you need to use *kwargs.

> I pretty much always prefer using an options struct

This is essentially what Vulkan does; there's a CreateInfo struct for every object creation or command function. And even then they managed to sort of mess it up, because they also have functions and objects suffixed with a '2', and the pNext extension mechanism.

I use this all the time in C (specifically, C99 or later). I first saw it used in anger in `sokol_gfx`, and loved it. `FLECS` does something similar.

C makes it much better than C++ in that the designated initializes can be set in any order --- so I don't need to remember the often-arbitrary order of struct fields for the options struct.

I find it weird that C++ took this great C feature and kneecapped it ...

I did this in my C project 7 years ago, as this is standard in C and gave a lot of readability, in fact more I guess... but a lot of preprocessor code too
Wouldn't that also mean you now need to define a new struct for every method that you want to do this with?