Settings

Theme

Things C++26 define_static_array can't do

quuxplusone.github.io

50 points by jandeboevrie 3 days ago · 31 comments

Reader

Pesthuf 15 hours ago

I love how to do anything in this language, you have to fight it with all you have. You have to use features in ways that feel like they were probably never intended to be used in. Everything is just an accident, some random pattern randomly discovered by someone when some typo happened to compile.

  • stodor89 10 hours ago

    > I love how to do anything in this language, you have to fight it with all you have. You have to use features in ways that feel like they were probably never intended to be used in. Everything is just an accident, some random pattern randomly discovered by someone when some typo happened to compile.

    This but unironically.

wewtyflakes 19 hours ago

This is so on-brand for C++, whether that is a blessing or curse is left up to the reader.

randusername 19 hours ago

> Think of constexpr evaluation as taking place "in the compiler’s imagination."

This is a great line.

constexpr and std::execution seem like neat ideas, maybe I'll give them a shot if I build an AI harness around the compiler so it doesn't make me feel like a hopeless idiot for trying new things.

  • tialaramex 17 hours ago

    The problem isn't so much that you feel like an idiot, at least at the time, but that you may think you're a genius and yet actually what you wrote was nonsense and the C++ compiler was under no obligation to tell you about that, indeed in many cases it's forbidden from doing so.

    The standard does require that if work was done at compile time the compiler is supposed to tell you if that was nonsense but (a) actually C++ is so complicated your compiler likely has many bugs in this respect and (b) you probably aren't sure the compiler did the work you expected at compile time, knowing all the excuses requires considerable expertise.

ghm2180 19 hours ago

To be clear we are — in the service of speed — trying to bake data into compiled program output binaries, which is ostensibly faster because code-pages in memory when executed by CPU as instructions already have it?

  • Maxatar 19 hours ago

    No you are baking data into the compiled program so that you can perform compile time operations on that data. These compile time operations are most often used to guard against erroneous runtime behaviors.

    If all you want to do is bake data into a compiled program, there is the #embed feature added to C++26.

  • TuxSH 17 hours ago

    The point of the article and of define_static_array is to convert stuff like constexpr std::vector to constexpr std::array.

    New (and delete) can be used in constexpr functions, however memory "allocated" like that cannot leave the constexpr "sandbox" so to speak, therefore std::vector cannot be generated at compile-time, but std::array may.

    If you are working with fixed-size data like LUTs, just use std::array [1]

    [1] Make sure not to use std::to_array when embedding 200KB+ files, as it's a mere constexpr function and not a language construct and will exceed constexpr limits; either specify the size or use a C-style array in this case

ok123456 19 hours ago

Would the PMR variants of the STL datatypes be a solution here? The compiler could fill in the details of the range in question (the ro section), and give you constexprness?

  • Maxatar 19 hours ago

    No, compile time memory is not the same as static storage duration/ro section.

oseityphelysiol 21 hours ago

C++ people are great at making problems for themselves and then solving them to no end.

This does not look like a productive way to get things done.

  • SuperV1234 20 hours ago

    C++ critics are great at analyzing niche corner cases of the language and generalising those to the entirety of its feature set.

    This does not look like a productive way to get things done.

    • imtringued 7 hours ago

      It's way less productive to spend significant time on criticizing something you don't use. Better spend that type on niche corner cases than be busy with the entire language.

  • kevin_thibedeau 20 hours ago

    C arrays are better than this mess. All the problems caused by the intervening generic programming abstraction machinery disappear.

    • compiler-guy 20 hours ago

      The nice thing about C++ is that if you want to use plain old C arrays, you can. And there will be no extra overhead over plain C.

      You will lose many nice features like fancy strings and easy array resizing (which may or may not be acceptable to you), but you don’t have to pay for it if you don’t use it. (Mostly)

      This does seem pretty complicated. And I doubt I will ever use it. But for some the trade off is worth it, and they get to make the choice.

    • SuperV1234 20 hours ago

      Didn't know you could use C arrays to get static compile time reflection, guess C was really ahead of its time!

  • gkedzierski 20 hours ago

    What language do you consider to be productive, in comparison?

    • htobc 20 hours ago

      Rather than engage in an unrelated language-war flame bait, why not actually discuss this particular issue with C++? Yes having a language built around zero cost abstractions for low level programming is a must, but how does that end up justifying this wild complexity around making constexpr more powerful?

      In the real world I would think trying to do any of the things discussed in this article should be an automatic commit rejection on any project.

      • SuperV1234 20 hours ago

        This particular issue is a niche corner case of C++26 reflection, which -- like reflection in other languages -- is a massively useful feature.

        In the real world, failing to understand what you're reading and eagerly generalising to the entire language should be an automatic hiring rejection in any team.

        • ux266478 18 hours ago

          i'd like to point out that C++26's reflection is the third reflection standard defined by the language to cover the "niche corner cases" that have hereto been lacking from the other two (RTTI and type traits). this specific "niche corner case" also would not exist if C++ did not commit to a poorly-bolted on feature that turned out to be accidentally powerful, and instead intentionally designed powerful metaprogramming facilities from day 1.

          there's a point at which "pragmatism" starts being anything but, and it was around C++11 give or take a standard. how on earth do you use it day to day and not feel the schizophrenic non-design being a generalized property across the whole language?

        • htobc 17 hours ago

          Hey buddy, maybe not liking something is not the same thing as not understanding it? Maybe saying, "this specific feature is bad" is not a generalization to the entire language? Maybe niche corner cases are evidence of poorly chosen primitives and bad design? Maybe jumping straight to smarm and skipping past actually defending the feature means you probably create a work environment no one wants to be in? And an esoteric paradigm like "constexpr two-stepping" that is explained in the article by linking a video that is _over an hour long_ is a perfect example of something that, while perhaps the author and demonstrator explored more for fun instead of as a serious thing to do, would only ever be put into a production code base by the most amateur of architecture astronauts, shortly before their startup fails?

          For real though, defend constexpr two-stepping as a real use case for serious people.

          Or did you just get a little bit confused and think the criticism here is actually coming from people who are out of their depth from hearing "compile time optimization" or don't know what reflection is?

          • SuperV1234 an hour ago

            > would only ever be put into a production code base by the most amateur of architecture astronauts, shortly before their startup fails?

            Yep, definitely failure of understanding. :)

            > For real though, defend constexpr two-stepping as a real use case for serious people.

            Of course, here's a use case: I am a serious person developing a library that provides a nice API to solve a real-world problem using C++26 reflection. As part of the internal library machinery, I need some temporary storage for some compile-time algorithm (e.g. building a graph for automatic parallelization, or some other thing like that). In an internal helper function of my library, I use constexpr two-stepping to solve the problem without imposing hard limits on my algorithms and keeping the final API as simple as possible for the end user.

            Then I submit my PR, but htobc reviews it and immediately rejects it, ignoring the real business value of the library because I made a conscious engineering decision to use a niche technique to solve a language limitation as part of my library implementation.

            Then my startup fails.

WalterBright 18 hours ago

Let's try this:

    constexpr std::vector<int> f() { return {1,2,3}; }
    constinit std::vector<int> p = f(); // error
in D!

    const(int)[] f() { return [1,2,3]; }
    immutable int[] aaa = f();
And the object file, look ma, aaa[] is statically allocated:

    internal:
        db      001h,000h,000h,000h,002h,000h,000h,000h ;........
        db      003h,000h,000h,000h,000h,000h,000h,000h ;........
    __D5test63aaayAi:
        db      003h,000h,000h,000h,000h,000h,000h,000h ;........
        db      000h,000h,000h,000h,000h,000h,000h,000h ;........
  • tialaramex 16 hours ago

    But why though? What is the purpose of the immutable "dynamic" array in your D?

    We're unavoidably going to learn at compile time how big the array is, and knowing how big it is will always be the same or better, so we should always do that.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection