77 points · by joshcsimmons · 1w ago · 47 comments
FooParams fooParams; // Gives sensible defaults to all members
fooParams.x = 1;
fooParams.y = "xyz";
foo(fooParams);
It doesn't work so well if you want to move construct things because you need to move at least twice: once when moving the value into the member, then you need to move the whole struct to the function argument `foo(std::move(fooParams))`. If you want it to copy something but avoid unnecessary copying then it's even more annoying because you need to move to the function argument still which looks odd when your goal is to copy overall. struct FooParams {
int x = getX();
const char* y = getY();
};
It turns out this was an issue all the way back in C++11, which didn't even have designated initialisers, but inline initialisers like this prevented the struct from being an aggregate, which meant you couldn't write FooParams{1, "y"}. That was fixed in C++14 (as I've just discovered).This isn't usually a huge deal, because C++ gives you other ways to have overloaded functions and default parameters, but it is sometimes mildly annoying.
I don’t see how that’s an arbitrary change. C++ initializes members in order of the definition of a class.
Making reading order follow that IMO is the right thing to do (and yes, they didn’t do that for class initializer lists (https://en.cppreference.com/cpp/language/constructor: “The order of member initializers in the list is irrelevant”), but that’s widely considered a mistake (https://www.intel.com/content/www/us/en/docs/dpcpp-cpp-compi..., https://learn.microsoft.com/en-us/cpp/error-messages/compile...)
(For the uninitiated, taglists are freeform lists of TAG / value pairs, which you would typically supply through varargs functions - something like OpenScreenTags(&newscreenstruct,SA_WIDTH,320,SA_HEIGHT,256,SA_DISPLAYID,myscreenmode,TAG_DONE); )
> I've often wondered where it came from
Don't know but the other place where I've seen this idea is when initializing OpenGL contexts on some platforms. This involves defining attribute key/value pairs like this:
int attributes[] =
{
GLX_RED_SIZE, 8,
GLX_GREEN_SIZE, 8,
GLX_BLUE_SIZE, 8,
GLX_ALPHA_SIZE, 8,
GLX_DEPTH_SIZE, 24,
GLX_STENCIL_SIZE, 8,
GLX_DOUBLEBUFFER, True,
None
};
Of course OpenGL is a bit later than AmigaOS, but not by much :)I started learning C on an Amiga with OS 1.3, then moved to a 2.0 machine. Fun times.
https://docs.vulkan.org/refpages/latest/refpages/source/VkDe...
As I mentioned there, I think it's even cooler when you collapse the "constructor" macro, and tag to be the same name.
You still get the advantage of not scrambling the order of your same-typed arguments.
It's more verbose and a different calling convention, but it might be worth it.
struct foo { int Z; int A; double E, double v };
#define DEFAULT_FOO_PARAMS .Z = 1, v = 3e8
foo_fn ( &(struct foo){ DEFAULT_FOO_PARAMS, .Z =2, .A = 4, .E = 1 });
But I do sort of wish there was a language-supported way to set defaults for a designated initializer in the struct definition (like C++ default values) to avoid having to write DEFAULT_FOO_PARAMS there.- the old Icon to-C compiler
- PuTTY's co-routines (and Simon Tatham's essay on C co-routines[0])
[0] https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
#include <stdio.h>
static int generator(int st, float *out) {
switch (st++) {
case 0: *out = 1.0f; return st;
case 1: *out = 7.0f; return st;
case 2: *out = 9.0f; return st;
}
return 0;
}
int main(void) {
float f;
for (int st = 0; (st = generator(st, &f));) {
printf("generator yielded %g\n", (double)f);
}
return 0;
}
but notice you also have the option to make the state static rather than a parameter, which gives you more encapsulation of your state machine at the cost of being way less flexible in terms of threading and reentrance.I've been using this style for almost 10 years now in the sokol headers, and it still feels like the right thing to do and actually more ergonomic than most other languages for initializing complex structs, e.g. see here for creating a 3D render pipeline object with dozens of creation options, but most of them keeping their default values:
state.pip = sg_make_pipeline(&(sg_pipeline_desc){
.layout = {
.attrs = {
[ATTR_texcube_pos].format = SG_VERTEXFORMAT_FLOAT3,
[ATTR_texcube_color0].format = SG_VERTEXFORMAT_UBYTE4N,
[ATTR_texcube_texcoord0].format = SG_VERTEXFORMAT_SHORT2N
}
},
.shader = shd,
.index_type = SG_INDEXTYPE_UINT16,
.cull_mode = SG_CULLMODE_BACK,
.depth = {
.compare = SG_COMPAREFUNC_LESS_EQUAL,
.write_enabled = true
},
.label = "texcube-pipeline"
});
(or see here: https://github.com/floooh/sokol-samples/blob/11bbe1a6865e93c...)...this code snippet also makes use of C99 features that are not available in C++20 though (or most other languages), and it's really remarkable how well designed the C99 designated init feature as a whole is (even though it was an afterthought) and how badly its individual subfeatures were understood by most later languages (basically, only Odin got it "mostly right": https://github.com/floooh/sokol-odin/blob/7d47a31cad83a26ba1...)
I had never seen array designated initializer combined with the struct variant. Great foresight of whomever thought to make these features so flexible and composable:
.attrs = {
[ATTR_texcube_pos].format = SG_VERTEXFORMAT_FLOAT3,
[ATTR_texcube_color0].format = SG_VERTEXFORMAT_UBYTE4N,
[ATTR_texcube_texcoord0].format = SG_VERTEXFORMAT_SHORT2N
}