C++ Weekly - Ep 443 - Stupid constexpr Tricks

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

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

  • @somerandompersonintheinternet
    @somerandompersonintheinternet 4 หลายเดือนก่อน +52

    0:39 yes, my coworkers definitely need some training. Not me, of course, I already know all there is to know about C++ :p

    • @cppweekly
      @cppweekly  3 หลายเดือนก่อน +2

      Tell your team lead to email me :D

  • @arthurmoore9488
    @arthurmoore9488 3 หลายเดือนก่อน +3

    I actually didn't find anything but the lambda to be strange at all. Even then, when you explained it , the lambda made sense to me. All you have to do is consider constexpr as a formalization of compile time optimizations.
    const tells the compiler the value will not change. Therefore, compilers are free to substitute a variable for the constant data, and it always makes sense to do so for ints.
    As a test, I ran the make_value example on C++03 with "-O1." Just switching auto to int, constexpr to const and static_assert to assert. The main function stays 2 instructions!

  • @MalcolmParsons
    @MalcolmParsons 3 หลายเดือนก่อน +1

    The unused lambda capture warning is checking whether the variable was ODR-used. During code review, I was told to remove the phrase ODR-used from the warning message.

  • @von_nobody
    @von_nobody 4 หลายเดือนก่อน +8

    Strange? This was default how you could handle compile time constants in older code, `constexpr` is more a normalization and be more explicit of this behavior.

  • @Yulenka-
    @Yulenka- 3 หลายเดือนก่อน

    The captures thing is not surprising if you remember that lambda captures correspond to member fields of a lambda object. Since constexpr values are known at compile time, they don't require to be stored/loaded at run time and thus they don't need a member field.

  • @wojtekburzynski654
    @wojtekburzynski654 3 หลายเดือนก่อน +1

    surprising thing for me is that you don't need to capture those variables. Rest I expected to work because 42, x = 10, and 20 is known at compile time, so result of lambda is also known at compile time.

  • @aibou2399
    @aibou2399 4 หลายเดือนก่อน +2

    The no need to capture x is indeed strange.
    Trying something similar with a struct instead of int doesn't work.
    #include
    struct Pair{ int x; int y; };
    int main()
    {
    constexpr Pair x{20, 30};
    constexpr auto sum = [](){return x.x + x.y;};
    printf("%d", sum());
    return 0;
    }
    last line fails to compile on gcc 14.2 ('x' is not captured).
    Works if static is added to x definition.

    • @jharmer95
      @jharmer95 3 หลายเดือนก่อน +1

      You can think of it as the 'const' being replaced by the compiler with 'static constexpr'. Lambdas can refer to static variables without capture, just like globals.

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

    I guess it would be less surprising if x and value was moved outside of main. I would assume all global constants could be reached from within the lambda

  • @aniketbisht2823
    @aniketbisht2823 4 หลายเดือนก่อน +34

    Jason, the bots find your videos "very informative and interesting". I hope that's encouraging to you.

    • @destiny_02
      @destiny_02 4 หลายเดือนก่อน +4

      Devin AI is evolving, and its coming for your job...

    • @warpspeedscp
      @warpspeedscp 3 หลายเดือนก่อน +1

      Apparently his memes inspire them too

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

    Well that was fun!

  • @yoavmor9002
    @yoavmor9002 4 หลายเดือนก่อน +10

    Not particularly surprising except the implicit capture of constexpr locals. Feels like some Java type stuff

    • @jharmer95
      @jharmer95 3 หลายเดือนก่อน +2

      You can think of it as the 'const' being replaced by the compiler with 'static constexpr'. Lambdas can refer to static variables without capture, just like globals.

  • @PaulMetalhero
    @PaulMetalhero 4 หลายเดือนก่อน +1

    Are we eventually going to move everything from constexpr to consteval?

    • @Voy2378
      @Voy2378 4 หลายเดือนก่อน +5

      no, many constexpr functions, like all from are also usable at runtime, not only at compiletime

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

      @@Voy2378 right

  • @PedroOliveira-sl6nw
    @PedroOliveira-sl6nw 4 หลายเดือนก่อน +3

    Is it implicit or is it because of the static_assert? I would imagine that static_assert forces the compiler to do more work and check if the condition can be known at compile time; at which point, the compiler would check if result can be known at compile time; and it back propagates to the two initializations, which can be known at compile time.
    I think of this as the namespace lookup rules or SFINAE kind of approach. It tries until it fails or succeeds to make it constexpr in its entirety.

    • @ohwow2074
      @ohwow2074 4 หลายเดือนก่อน +1

      I think it's implicit. Remove static assertion and test by other means and you'll see that const simply means constexpr in these cases.

    • @BitZorg
      @BitZorg 4 หลายเดือนก่อน +1

      Recall that constexpr only means that it can be computed at compile time but doesn't guarantee it is done then. So implicit is accurate even if the static_assert is what forces the compiler to compute the value at compile time.

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

      it's implicit, but we need the static_assert to actually see what it did.

  • @HowlingDeath
    @HowlingDeath 4 หลายเดือนก่อน +1

    Any can pass standard reference to this pls??

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

    I'm not quite certain how std::function or lambdas are implemented, but to me the non-explicit capturing thing could make a little sense. At least in the sense, that constexpr values do not require any dynamic state to be bound to a function object (kind of obvious for primitive types, not sure about more complex types).

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

    Do you have any online training course for intermediate coder?

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

      Sorry, I don't really do online training or mentorship. The best I can offer is that you become a Patron (links in video description) and join my Discord. You can ask questions there and I do my best to answer as soon as possible.

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

    Thanks! I'll try not to abuse this knowledge :-)

  • @ConceptInternals
    @ConceptInternals 4 หลายเดือนก่อน +3

    In this example, if we return the lambda from the function which refers to local constexpr variable, is calling that lambda an undefined behaviour?

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

      It is not - no object has been captured - only the value has been "captured" (it's going to behave more like a #define internally)

  • @TsvetanDimitrov1976
    @TsvetanDimitrov1976 4 หลายเดือนก่อน +2

    Is this subject to compiler optimization options? I mean #pragma optimize( "", off ) should disable this behavior, right?

    • @Spielix
      @Spielix 4 หลายเดือนก่อน +3

      Not if it is a language feature... which it apparently is.

    • @cppweekly
      @cppweekly  3 หลายเดือนก่อน +1

      @@Spielix Correct - language feature.

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

    I don't understand the logic behind implicit constexpr capture in lambdas.
    IMO, rules of language should contain as less as possible "if"s in their definition.

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

      constexpr values are compile-time constants.
      lambda captures change the size and shape of the lambda, it must now have storage for the values and they are no longer implicitly convertible to function pointers. I think this is a good and logical decision.

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

    nonsensical! (😉) But very interesting. Always more to learn with C++, thanks Jason!

  • @bean_TM
    @bean_TM 4 หลายเดือนก่อน +9

    The bots are fast wtf

    • @LewiLewi52
      @LewiLewi52 4 หลายเดือนก่อน +9

      They are just big C++ weekly fans.

    • @m0r17y
      @m0r17y 4 หลายเดือนก่อน +6

      No wonder, considering how informative and informative these videos are

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

      That's why they are botnics! but what is the point of those?

  • @peternimmo74
    @peternimmo74 4 หลายเดือนก่อน +9

    Loads of background sound on this particular video.

    • @PRIMARYATIAS
      @PRIMARYATIAS 4 หลายเดือนก่อน +1

      Ok Mr AI Bot

    • @peternimmo74
      @peternimmo74 3 หลายเดือนก่อน +1

      Say what? How do you work that one out? Did you listen to the video? It's noisey, and they usually aren't.

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

      @@peternimmo74 I might have missed a filter. Sorry about that!

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

    I'm sure you would expect me to say that I would expect that kind of behavior by default anyway and this is how my own language works, but minus a lot of that unnecessary verbiage. For instance, in my language there is no constexpr or consteval and no need for them as everything that can be is evaluated at compilation time, and one can use const to tag a block of code or whatever you want to assign an expression to and the only reason to bother tagging something as const is to error check yourself and your assumptions about what can be evaluated at compile time. Basically, if you tag something as const in my language and it can't be evaluated at compile time, then you get an error pointing out where it can't be. As an example, even though I doubt anyone will read this or be curious, if you have `const { i := some_library_function(); puts( "{i+10}
    " ); }` then it'll be an error if the compiler can't see inside that library and evaluate it.

  • @kashgarinn
    @kashgarinn 4 หลายเดือนก่อน +3

    integers, not integrals 😅

    • @Spielix
      @Spielix 4 หลายเดือนก่อน +8

      Integral is correct/more general. See std::is_integral.

    • @PRIMARYATIAS
      @PRIMARYATIAS 4 หลายเดือนก่อน +1

      I have encountered some math textbooks that called integers/whole numbers as integral numbers so I guess it is fine

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

      @@Spielix I would say somewhat less correct. It is linguistically non-standard while not conveying additional information. Secondly, specific items are integers while an integral quantity is an amount representable as an integer in the abstract.

    • @Spielix
      @Spielix 3 หลายเดือนก่อน +3

      @@mytech6779 In this context integral is just short for integral type and not all integral types are commonly called integers (e.g. chars and bools). So if you mean integral types saying integers would be less correct.

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

      @@mytech6779 Truthfully, it's a quick and dirty way of not specifying the type of integer that it is no matter which way it's said. Sadly, the standard has changed enough on this particular issue that it could be invalidated easily by a future revision for him to say what size integer it actually is. However, I would consider it equally as correct to say integral or integer, but integral fits more here in the context in which he was talking because he wasn't intending to specify a specific integer despite using one as an example.

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

    god this all just looks cursed
    like something that will immediately put off experienced and new developers alike
    especially the lambda capture thing, that just seems confusing

    • @hijarian
      @hijarian 3 หลายเดือนก่อน +2

      welcome to the Javascript world where all captures are implicit lol

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

      @@hijarian Same with C#, Python, and pretty much any interpreted language.

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

      @@arthurmoore9488 in C# and Python lambdas implicitly capture everything from the enclosing scope?!?!?! holy cow what a mess... Even PHP didn't do that when it added lambdas back then in v5.

    • @toby9999
      @toby9999 วันที่ผ่านมา

      It's probably not a topic for beginners. I've been using C++ for 30 years and never used any of this stuff. Most of the codebases I've worked on a pretty much C++98 syntactically, even though we're using modern compilers.

    • @bruderdasisteinschwerermangel
      @bruderdasisteinschwerermangel 15 ชั่วโมงที่ผ่านมา

      @@toby9999 when you say syntactically I guess (or rather hope) that you still use some of the newer features (especially std library)