Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

In C++ I very often find myself wanting to have the ability to define a type inline - e.g. my ideal syntax would look like

    int z = 1;
    auto x = [=] { .foo = z, .bar = {4,5,6}, .baz = "" };
Combined with the ability to introspect things at compile-time available in c++20, I think that this would get 99% there for common data-oriented design designs, without the need for a completely new programming language such as Ballerina.


"Easy" to do if you are willing to put up with macros:

   https://github.com/gpderetta/libtask/blob/master/tests/q_test.cpp#L160
Note the macro is $, tup is just a C++ function. It doesn't handle your .bar case, but would be very easy to extend tup to handle initializer lists.

(No, I do not use it in production, it is mostly a fun curiosity, but yes, I wish lightweight named tuples were part of the language).


I imagine this creates a std::tuple, which causes a lot of template instantiations and is the main offender for debug compile-times though. But yes, that's pretty much what I'm looking for :-)


It is not an std::tuple, but the moral equivalent. I haven't tested the compile time beyond trivial tests, but it not going to be great. The $ macro itself expands to a non-trivial amount of code.




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

Search: