Why use templates in modern C++

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

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

  • @theintjengineer
    @theintjengineer 9 หลายเดือนก่อน +5

    Templates, Concepts, Fold Expressions, constexpr if, etc. are really neat.
    If you understand your requirements, you can craft good concepts [or use the standard ones], and then, although you might still get some long error messages, the compiler will point right at the part that messed up the template. That's really cool.

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

    I think the biggest cost that we pay for using templates is the huge error messages that you get if a type you provide is not compliant with the template code. C++ 20 concepts improve this but don't solve the issue entirely.

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

      I would say that the main cost of templates (and concepts by extension) is not the long error messages. These are just inconvenient but not a show stopper. The main issue is the compilation time as I see it. And that too only sometimes. But they bring a lot of benefits. Later I plan to talk about alternative designs with their benefits and issues.

    • @zamf
      @zamf 9 หลายเดือนก่อน +1

      @@CodeForYourself I guess it really depends on how much time you spend waiting for compilation and how much time you spend trying to understand what the error message is telling you.

    • @CodeForYourself
      @CodeForYourself  8 หลายเดือนก่อน +1

      To a degree. When I’m talking about long compile times I’m mostly talking about many hours to days to build the full code base. But this type of issues only happens when one has a code base of like Google scale. Overly generic templates in a wrong place in such a code base can easily lead to lost hours. But then again, this is something out of the ordinary.

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

    Just use `auto` 😂

    • @CodeForYourself
      @CodeForYourself  9 หลายเดือนก่อน +2

      Well, technically auto uses a very similar mechanism and going forward we will be able to do just that and it will actually be equivalent to using templates. I still think it is important to understand the basics here though 😅