7 ms·
C++26: Pack Indexing
- foota 2y agoThis is nice, but I'm still waiting for a constexpr for loop to use it with.
- mystified5016 2y agoCan we have constexpr math functions like sqrt already?
- Maxatar 2y agoMy understanding is that all of the functions found in the C compatibility headers that the C++ standard defines can not be made constexpr. sqrt is among them, as are a lot of the math functions.
- agosz 2y agoThe compatibility can be solved with macros to toggle it on or off depending on how you're building. C++26 has constexpr sqrt.
- pjmlp 2y agoThere is a paper suggesting to replace all macro based code, due to way modules work. https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p2654r1.pdf https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p26...
- stefanos82 2y agoSeems like since C++26, it does use constexpr https://en.cppreference.com/w/cpp/numeric/math/sqrt https://en.cppreference.com/w/cpp/numeric/math/sqrt
- logicchains 2y agoIf you want to iterate over the whole thing you can just use a fold expression, which arguably look cleaner than the non-compiletime ways to map over a collection like std::transform.
- a_t48 2y agoEven with fold expressions, sometimes you need the actual index. See here https://github.com/basis-robotics/basis/blob/ca70aee6daca37ccb3997db56923adc1601ed455/cpp/unit/include/basis/unit.h#L230 https://github.com/basis-robotics/basis/blob/ca70aee6daca37c... - I did use a fold expression, but I needed the actual index in the fold expression to both do array access and to pass down into a template parameter.
- almostgotcaught 2y agoBro fold expressions are great and all but Jesus Christ the name spacing in that file. Like have you ever heard of this hidden C++ feature called `using namespace`?
- a_t48 2y agoYes, and? It's much easier to remove namespaces later using `using namespaces` if it really becomes a maintenance hazard than to go through and add them all back. This is library code that was intended to be included with unknown customer code, part of the goal is to avoid namespace collisions. (The `basis::` prefixes should go, though, the entire file is inside `basis`)
- almostgotcaught 2y ago> It's much easier to remove namespaces later using `using namespaces` lol how? sed? that's not a "much easier", that's a hack. it's actually much easier to remove `using namespace` because then the compiler will catch resolution errors. i actually didn't notice this was a header in which ok fine don't use `using namespace` but still this is java EE levels of boiler plate you've got going on; void SetupInput(const basis::UnitInitializeOptions &options, basis::core::transport::TransportManager *transport_manager, basis::core::containers::SubscriberQueueSharedPtr &subscriber_queue, basis::core::threading::ThreadPool *thread_pool, const std::unordered_map<std::string, std::string> &templated_topic_to_runtime_topic) { this signature is 90% namespace resolution - i have to squint to actually find the types...
- a_t48 2y agoPretty please. [&]<std::size_t... I>(std::index_sequence<I...>) { (SetupInput<I>(options, transport_manager, subscriber_queues[I], thread_pool, templated_topic_to_runtime_topic), ...); }(std::make_index_sequence<INPUT_COUNT>()); would be a lot more readible as for constexpr( size_t I = 0; I < INPUT_COUNT; I++ ) SetupInput<I>(options, transport_manager, subscriber_queues[I], thread_pool, templated_topic_to_runtime_topic); } Yes, there's some syntax snags around it (right now I looks mutable, something like `for (constexpr size_t I : INPUT_COUNT)` might be better), but there has to be some sane middleground.
- moonshadow565 2y agoP1061 is in C++26 so you can instead do: const auto [...I] = std::make_index_sequence<INPUT_COUNT>{}; ((SetupInput<I>(options, transport_manager, subscriber_queues[I], thread_pool, templated_topic_to_runtime_topic)),...); yay!
- spacechild1 2y ago> right now I looks mutable, something like `for (constexpr size_t I : INPUT_COUNT)` might be better I thought the same, but then you lose the ability to control the increment step. For example, one might want to iterate pairwise. Regarding syntax, you could mandate that the loop variable has to be declared `constexpr` as well, which makes it clear that it can't be modified in the loop body.
- a_t48 2y agoYou could make a temporary or use some std::range/span helper. Double constexpr would work, too. Feels gross in the same way template template does.
- pragma_x 2y agoIt's been ages since I've done metaprogramming with C++. I had to look this up, since my first reaction was: just use recursion! Yeah, I can see why a for-loop is the preferred way to go here. Turns out that const-call depth is limited to 512, but by implementation, not by the standard (which is undefined). https://stackoverflow.com/a/9320911/713831 https://stackoverflow.com/a/9320911/713831
- foota 2y agoThere's a way to "iterate" over parameter packs called fold expressions, but they're painful and look very weird.
- plorkyeran 2y agoI don't find them painful, but they're definitely extremely weird looking. The fact that they're "fold expressions" but are normally used with operator comma to sequence together things that want to be statements is a pretty strong sign that something went wrong in the design process.
- a_t48 2y agoWhen I first touched this around C++14 or so, I used recursion. It resulted in even worse code.
- beached_whale 2y agotemplate for is proposed for 26 as part of the reflection papers.
- BoingBoomTschak 2y agoMeanwhile, in CL/Clojure: everything is constexpr with no syntax difference. You can even do I/O at macro-expansion time. And I say that as someone using C++17 at work, not just a smug lisp weenie.
- edflsafoiewq 2y agoC++ continues its long journey to making its template system an actual usable programming language.
- kubb 2y agoIt's unfortunate that it had to happen by accident, and now new features are reluctantly being pushed through piecemeal. Imagine what it could have been if it was designed for programmability from the start.
- klipt 2y ago> Imagine what it could have been if it was designed for programmability from the start. Circle C++ compiler was designed for that: https://www.circle-lang.org/quickref.html https://www.circle-lang.org/quickref.html
- nxobject 2y agoI do think as well that some deeper scrutiny of semantics with the tools of PL research might have helped ahead of time - some of the most glaring warts like the perfect forwarding problem, or the need for reference collapsing, would have been avoided if the standards committee had taken a more rigorous approach to understanding semantics and their implications. I hope Zig’s comptime will get that level of scrutiny.
- kazinator 2y agoIt would have been a huge, sudden increment in complexity from absolute zero, causing it to be forever written off and ignored as an incomprehensible curiosity looking for a practical application. You have to suck people in with, "hey, you can write these two similar C-like functions under one definition, the correct one of which is automatically deduced and used".
- papichulo2023 2y agoI like to code in cpp as a hobby (computer vision stuff) but avoid template programing like the plague. The part of std is already scary stuff with 50 lines of errors for single typo.
- Kenji 2y ago[dead]
- suby 2y agoI don't have a source, but I have read that the reason this wasn't originally introduced into C++ was because they couldn't agree on what the syntax would be. So much pain because the committee is dysfunctional, it took a decade plus to introduce something that everyone from the start wanted and understood would improve things. Par for the course with C++.
- rerdavies 2y agoAt least it's progress over the first decade of C++'s life where NOTHING happened.
- spacechild1 2y ago> I don't have a source, but I have read that the reason this wasn't originally introduced into C++ was because they couldn't agree on what the syntax would be I can totally imagine that. That's double ironic because the syntax is pretty obvious (which I find pretty surprising for a new C++ feature :) But yeah, it's amazing that we only get this 15 years(!) after the introduction of variadic templates. I can totally see why some languages would decide against ISO standardization.
- mkoubaa 2y agoSuppose c++ is around for 100 more years (i'll take the over). Sacrificing a couple decades in order to get the syntax just right is worth it. Just look at... Javascript.
- a_t48 2y agoI usually find myself putting parameter packs into tuples and then using `std::get` on the tuple to index. It's not perfect (will end up making copies), but has the side benefit of being able to additionally store the parameter pack for later.
- int_19h 2y agoZig's approach to generics and metaprogramming - treating functions and types as first-class compile-time values in the base language - makes more and more sense.
- psyclobe 2y agoC++ parameter packs are really bad ass, super cool to see this added
- rurban 2y agoMore insane... syntax from the C++ committee. How about a readable function-like keyword, like any other self-respecting language? C++ surpassed APL and Perl by far already. Really, in APL there is some consistency at least.
- foota 2y agoWhy would we need a function keyword?
- 3836293648 2y agoYou don't strictly need it, but you get rid of a bunch of ambiguities in the grammar. Not that it'd solve all the ambiguities in C++. Is a<b, c>(d) a function call or two comparisons combined with the stupidest operator in the language? Impossible to know without type information. (This is also why rust has the super ugly ::<T> for generics, so that it's unambiguous)
- joe91 2y agoarray[index] is insane syntax? That's all that's being talked about here (allowing array-like indexing of parameter packs which are variadic template parameters).
- joe91 2y agobtw: you can actually write, for example, the first function as just: auto first(auto... params) { return params...[0]; } Unless I'm missing something - but the post is showing off the fact that you can index into the list of types of the parameters, as well as the parameters themselves (in which case you need full template syntax).
- rurban 2y agoWhat I said. ... as type and identifier suffix is just nonsense. A proper language would have used ... to designate varargs, and a special identifier to hold these args. Just as the macro preprocessor.
- coolThingsFirst 2y agolol C with STL would've saved the world a lot of trouble.
- otabdeveloper4 2y ago> just write a half-assed code generator tool that uses void* under the hood, bro Not the kind of "simplicity" I want. Stay in your reservation, please.
- binary132 2y agoThe griping in the comments here is insane considering that being able to easily static_assert(first(1, 2, 3, 4, 5) == 1) is a pretty powerful language feature.