Understanding The constexpr 2-Step - Jason Turner - C++ on Sea 2024

แชร์
ฝัง
  • เผยแพร่เมื่อ 24 พ.ย. 2024

ความคิดเห็น • 8

  • @kimhyunpil
    @kimhyunpil 2 หลายเดือนก่อน

    If you're looking for code that generates a string at compile time, you definitely need to watch this lecture.👍

    • @SkegAudio
      @SkegAudio หลายเดือนก่อน

      just got started with cpp. What's the significance of this feature?

    • @kimhyunpil
      @kimhyunpil หลายเดือนก่อน

      ​@@SkegAudio Using constexpr string, you can get faster and more predictable code.

    • @ensuretime
      @ensuretime หลายเดือนก่อน

      @@SkegAudio you lose in compilation time, but you gain in not having to compute the values every time the binary runs... in other words, the more you can leave to the compiler to do, the better... you don't pay at runtime for it

    • @codures
      @codures 7 วันที่ผ่านมา

      ​@@SkegAudio you forget it exists. it doesn't make any sense *yet* if you are in the "hello world".
      TLDR: it tells the compiler to arrange the binary layout 😮 assembler code in such a way (reducing/optimizing) that whatever function/s you are calling should, if possible, be transformed into something that returns whatever you expected to first place, by performing the necessary computations at compile time. it's always a trade-off between readability, speed, size and skill.

  • @scion911
    @scion911 2 หลายเดือนก่อน

    This is a type of problem I reached while doing things in constexpr, essentially you have to have MAX_LIMITS for oversized array, have a computation run once where you basically compute the size of the result (usually just computing the entire thing) then discard every result and just return the size. Then take the size as a constexpr then again run the computation in an appropriate sized array not to blow up the memory. This caused massive issues for compile times. I reached the same copy twice method for compile-time later, but unfortunately my usecase deemed c++ constexpr interpreter thing too slow.
    This led me to switch my library to zig comptime. Which has much better semantics than C++ unfortunately for compile time stuff. Not to mention it doesn't color functions as constexpr, it just runs it if possible to run it in comptime.