3 ms·
The "Galaxy Brain" version of this advice is using plain old designated initialization of 'option bag' data structures. Here's a non-trivial C99 example from th
by flohofwoe 5y ago
The "Galaxy Brain" version of this advice is using plain old designated initialization of 'option bag' data structures. Here's a non-trivial C99 example from the sokol_gfx.h API to create a pipeline-state-object for 3D rendering:
state.pip = sg_make_pipeline(&(sg_pipeline_desc){
.layout = {
.attrs = {
[ATTR_vs_position].format = SG_VERTEXFORMAT_FLOAT3,
[ATTR_vs_color0].format = SG_VERTEXFORMAT_FLOAT4
}
},
.shader = shd,
.index_type = SG_INDEXTYPE_UINT16,
.cull_mode = SG_CULLMODE_BACK,
.depth = {
.write_enabled = true,
.compare = SG_COMPAREFUNC_LESS_EQUAL,
},
.label = "cube-pipeline"
});
Chaining might be useful for data processing pipelines, but not for simple data initialization tasks IMHO.
- jimbob45 5y agoI know C is an extremely deep language but that doesn’t look like C99 to me. Since when can you set struct member variables piecewise in a…function definition? Is this maybe a custom compiler? Or is this actually C++?
- kevin_thibedeau 5y agoIt's a C99 compound literal with designated initializer. C++ doesn't support it.
- nwellnhof 5y agoWhat you're seeing is a compound literal with designated initializers. Both features were introduced in C99.
- rackjack 5y agoIt is C99, which surprised me too. https://stackoverflow.com/a/7487949/11736447 https://stackoverflow.com/a/7487949/11736447
- chisquared 5y agoI encountered code like this in a C99 project recently. I still don't understand it, but I believe it's supposed to be possible.
- MayeulC 5y agoAs other commenters have pointed out, those are designated initializers[1], one of my favorite C99 feature. I's also been picked up recently for inclusion into C++20[2]. However, it seems that C++ adds a specific limitation: > all designators used in the expression must appear in the same order as the data members of T > out-of-order designated initialization, nested designated initialization, mixing of designated initializers and regular initializers, and designated initialization of arrays are all supported in the C programming language, but are not allowed in C++ Which limits the usefulness, as I've used this syntax to let myself reorder struct fields (and add some) without breaking the API (while obviously breaking the ABI). It's a great tool to use while your API is still in flux. At least an error will be produced, which is much better than just providing values and hoping that the API/ABI remains stable. [1]: https://en.cppreference.com/w/c/language/struct_initialization https://en.cppreference.com/w/c/language/struct_initializati... [2]: https://en.cppreference.com/w/cpp/language/aggregate_initialization#Designated_initializers https://en.cppreference.com/w/cpp/language/aggregate_initial...
- matheusmoreira 5y agoDesignated initializers is the most amazing feature of the C language. I don't understand why no other language ever copied it...
- jmercouris 5y agoIt exists in Lisp, even before C.
- AtlasBarfed 5y agoGroovy has this ability I think if I can parse that code. My C code experience predates C99
- kevinmgranger 5y agoDidn't Swift adopt something similar? (I'm no Swift dev so I'm not sure)