Templates under the hood

Поділитися
Вставка
  • Опубліковано 16 гру 2024

КОМЕНТАРІ • 17

  • @zamf
    @zamf 10 місяців тому +8

    It is also worth mentioning that templates are usually defined in the header file since they can be used in multiple translation units. Because of this the linker can end up with multiple specializations with the same template arguments but it picks one of them and ignores the rest as it assumes that all of them come from the same template definition and therefore are identical.

    • @CodeForYourself
      @CodeForYourself  10 місяців тому +3

      Thanks for mentioning this! We will talk about header and source files soon. As well as all the details of how to write templates well and what linkage template functions have. Here I wanted to give an intuitive explanation of what the compiler does when it sees templates. Does this make sense?

    • @zamf
      @zamf 10 місяців тому +3

      @@CodeForYourself Absolutely. The explanation was great and clear. I'm probably rushing into the next video already :)

    • @CodeForYourself
      @CodeForYourself  10 місяців тому +4

      Yeah, I’m dreading preparing that video. Really a lot of things to think and tell about 😅

  • @bsdooby
    @bsdooby 10 місяців тому +3

    Looking forward to the series!

  • @KotlasBoy
    @KotlasBoy 8 місяців тому +1

    Great gratitude from 1 course student!

  • @longvuong2600
    @longvuong2600 10 місяців тому +1

    Ah very first view! 😁 Thank you for the video as usual!

  • @MateusDuarttee
    @MateusDuarttee 10 місяців тому

    Great video! One doubt, template metaprogramming, is just using templates inside templates?

    • @CodeForYourself
      @CodeForYourself  10 місяців тому

      I wouldn’t say so. I would say that template meta programming is writing logic using templates such that its result is available also at compile time. This way it can be used for conditional compilation of just parts of the code. Does that make any sense?

    • @MateusDuarttee
      @MateusDuarttee 10 місяців тому

      @@CodeForYourself yes, so when doing metaprogramming i'm not just trying to do generic programming, but actually do computational steps in compile time, that is right?

    • @CodeForYourself
      @CodeForYourself  10 місяців тому +2

      @@MateusDuarttee to a degree. They are not always related to the computation that you actually want to do in your main program but they have complex logic and their result must be available at compile time. One example I can give that I’ve done some time ago is this. I had an array of values that represented physical quantities. I was implementing an algorithm that would smooth such values over time by applying certain mathematical operations to the whole array. Now this can be quite error prone if we mix up the order of the values. So I used template meta programming to guard myself. Every value would have a type in association with it. These types would be ordered just like my values. Every operation then would check if the order of these indicator types corresponds to the expected order. All of this at compile time. So if I would try to update, say, speed in my array as if it were position by mistake I would get a compile time error that would protect me from making this mistake.
      Sorry for a slightly convoluted and hand-wavy example but that’s the best I can do for now. I think we will talk about this later too. Does it answer your question in any way?

    • @MateusDuarttee
      @MateusDuarttee 10 місяців тому +1

      @@CodeForYourself Thank you a lot for the answer!! Now i guess that the idea of meta programming is more clear in my mind, also congrats for the videos to, great job!

    • @CodeForYourself
      @CodeForYourself  10 місяців тому +1

      @@MateusDuarttee glad it helped!

  • @Partey-xh3fr
    @Partey-xh3fr 10 місяців тому +1

    Маєш файну футболку