title
Templates in C++
description
Patreon ► https://patreon.com/thecherno
Twitter ► https://twitter.com/thecherno
Instagram ► https://instagram.com/thecherno
Discord ► https://thecherno.com/discord
Series Playlist ► https://thecherno.com/cpp
Thank you to the following Patreon supporters:
- Samuel Egger
- Dominic Pace
- Kevin Gregory Agwaze
- Sébastien Bervoets
- Tobias Humig
- Peter Siegmund
Gear I use:
-----------------
BEST laptop for programming! ► http://geni.us/pakTES
My FAVOURITE keyboard for programming! ► http://geni.us/zNhB
FAVOURITE monitors for programming! ► http://geni.us/Ig6KBq
MAIN Camera ► http://geni.us/t6xyDRO
MAIN Lens ► http://geni.us/xGoDWT
Second Camera ► http://geni.us/CYUQ
Microphone ► http://geni.us/wqO6g7K
detail
{'title': 'Templates in C++', 'heatmap': [{'end': 378.781, 'start': 364.451, 'weight': 0.772}, {'end': 735.064, 'start': 635.416, 'weight': 0.868}, {'end': 790.316, 'start': 772.691, 'weight': 0.872}], 'summary': 'Covers c++ templates, emphasizing their power and flexibility in avoiding code duplication, showcasing their role in functions for generating code based on parameters, exploring automatic type deduction, and demonstrating the creation of stack-allocated arrays using templates.', 'chapters': [{'end': 228.178, 'segs': [{'end': 48.964, 'src': 'embed', 'start': 17.834, 'weight': 0, 'content': [{'end': 20.254, 'text': 'So, first of all, what is a template?', 'start': 17.834, 'duration': 2.42}, {'end': 25.356, 'text': 'Well, those of you familiar with other languages might see this as kind of generics, right?', 'start': 20.675, 'duration': 4.681}, {'end': 27.696, 'text': "So in Java or C sharp, they're kind of called generics.", 'start': 25.376, 'duration': 2.32}, {'end': 29.737, 'text': 'Templates are completely different.', 'start': 28.417, 'duration': 1.32}, {'end': 36.999, 'text': "well, they're not completely different, but they are like infinitely more powerful than generics in those managed languages.", 'start': 30.077, 'duration': 6.922}, {'end': 41.201, 'text': 'right templates are almost like a bit of a macro.', 'start': 36.999, 'duration': 4.202}, {'end': 48.964, 'text': 'they let you do pretty much anything, whereas generics is very heavily limited by the type system and by a lot of other factors.', 'start': 41.201, 'duration': 7.763}], 'summary': 'Templates in c++ are more powerful than generics in java or c#, offering more flexibility and functionality.', 'duration': 31.13, 'max_score': 17.834, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/I-hZkUa9mIs/pics/I-hZkUa9mIs17834.jpg'}, {'end': 108.696, 'src': 'embed', 'start': 82.031, 'weight': 1, 'content': [{'end': 85.891, 'text': 'You can get the compiler to write code for you based on a set of rules.', 'start': 82.031, 'duration': 3.86}, {'end': 90.852, 'text': "That's really what a template is, just the compiler writing code for you based on the rules that you've given it.", 'start': 85.911, 'duration': 4.941}, {'end': 98.194, 'text': "When I write a function, for example, and I use a template in that function, what I'm actually doing is creating almost like a blueprint,", 'start': 90.992, 'duration': 7.202}, {'end': 104.715, 'text': 'so that when I decide to call that function, I can specify certain parameters which determine what code actually gets put into the template.', 'start': 98.194, 'duration': 6.521}, {'end': 108.696, 'text': 'And that is all kind of determined by the way that I actually use that function.', 'start': 105.195, 'duration': 3.501}], 'summary': 'Using templates allows the compiler to generate code based on specified rules and parameters.', 'duration': 26.665, 'max_score': 82.031, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/I-hZkUa9mIs/pics/I-hZkUa9mIs82031.jpg'}, {'end': 147.733, 'src': 'embed', 'start': 116.58, 'weight': 3, 'content': [{'end': 117.921, 'text': "I'm trying not to do that, obviously.", 'start': 116.58, 'duration': 1.341}, {'end': 122.003, 'text': 'I think the best way is just going to be to show you what it actually looks like in the code.', 'start': 117.941, 'duration': 4.062}, {'end': 130.608, 'text': 'So a good example of why I might want to use a template is if I want to create a function that is largely going to be the same but may take in different types.', 'start': 122.043, 'duration': 8.565}, {'end': 134.549, 'text': 'Maybe I have a print function that I want to use to print integers.', 'start': 130.948, 'duration': 3.601}, {'end': 139.271, 'text': 'So I might do something like print int value, and all this is going to do is see out my value.', 'start': 134.609, 'duration': 4.662}, {'end': 144.372, 'text': 'And if I go back down to main, I can call print with a value such as five and everything will be fine.', 'start': 139.531, 'duration': 4.841}, {'end': 147.733, 'text': 'Now, what if I want to actually use a string there instead, like hello?', 'start': 144.772, 'duration': 2.961}], 'summary': 'Explaining the use of templates in code for different data types.', 'duration': 31.153, 'max_score': 116.58, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/I-hZkUa9mIs/pics/I-hZkUa9mIs116580.jpg'}, {'end': 228.178, 'src': 'embed', 'start': 189.663, 'weight': 2, 'content': [{'end': 196.646, 'text': "Because cout can of course accept any kind of primitive or or built in C++ type that we're actually using here.", 'start': 189.663, 'duration': 6.983}, {'end': 202.088, 'text': "So what we've ended up with is manually defining three different overloads for a print function.", 'start': 196.906, 'duration': 5.182}, {'end': 203.888, 'text': "That's okay.", 'start': 202.928, 'duration': 0.96}, {'end': 206.189, 'text': 'Some people actually prefer to do it this way.', 'start': 204.228, 'duration': 1.961}, {'end': 208.07, 'text': 'But what if we could just define that function once?', 'start': 206.389, 'duration': 1.681}, {'end': 214.292, 'text': 'A good goal for software engineering in general really is to kind of avoid code duplication, right?', 'start': 209.23, 'duration': 5.062}, {'end': 219.054, 'text': 'Because if we decide to actually change one of these print functions to do something extra,', 'start': 214.652, 'duration': 4.402}, {'end': 224.856, 'text': 'or suddenly we decide to go from using cout to printf or one of our own custom logging functions,', 'start': 219.054, 'duration': 5.802}, {'end': 227.657, 'text': "we have to change it in all of the places that we've actually defined.", 'start': 224.856, 'duration': 2.801}, {'end': 228.178, 'text': 'that right?', 'start': 227.657, 'duration': 0.521}], 'summary': 'Manually defined three overloads for a print function, aiming to avoid code duplication in software engineering.', 'duration': 38.515, 'max_score': 189.663, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/I-hZkUa9mIs/pics/I-hZkUa9mIs189663.jpg'}], 'start': 0.069, 'title': 'C++ templates and avoiding code duplication', 'summary': 'Introduces c++ templates, highlighting their power and complexity and their role in allowing flexible code compilation. it explains how templates can be used in functions to create a blueprint for generating code based on specified parameters and discusses the need to avoid code duplication in c++ by illustrating the complexity involved in managing multiple function definitions for different data types.', 'chapters': [{'end': 82.011, 'start': 0.069, 'title': 'Introduction to c++ templates', 'summary': 'Introduces c++ templates, highlighting their power and complexity, with the promise of future in-depth exploration, as it explains their differences from generics and their role in allowing flexible code compilation.', 'duration': 81.942, 'highlights': ['Templates in C++ are more powerful than generics in Java or C#, offering greater flexibility and functionality, making them a significant and complex topic in C++ series.', 'The introduction to templates in C++ promises a future in-depth exploration, acknowledging their complexity and potential for multiple videos dedicated to the topic.', 'Templates in C++ allow the definition of a template that will be compiled by usage, showcasing their role in enabling flexible code compilation.']}, {'end': 134.549, 'start': 82.031, 'title': 'Templates in code', 'summary': 'Introduces the concept of templates in coding, explaining how the compiler writes code based on specified rules and how templates can be used in functions to create a blueprint for generating code based on specified parameters. an example of using templates to create a function that can handle different types is also provided.', 'duration': 52.518, 'highlights': ['When using a template in a function, it creates a blueprint for generating code based on specified parameters, allowing for flexibility in determining the code that gets put into the template.', 'Templates can be utilized to create functions that handle different types, such as a print function that can be used for printing integers and other data types.']}, {'end': 228.178, 'start': 134.609, 'title': 'Avoiding code duplication in c++', 'summary': 'Discusses the manual definition of multiple overloads for a print function in c++, illustrating the need to avoid code duplication and the complexity involved in managing multiple function definitions for different data types.', 'duration': 93.569, 'highlights': ['The need to define multiple overloads for print function based on data type (integer, string, float). The chapter explains the necessity of defining separate overloads for the print function for different data types like integer, string, and float.', 'Demonstration of code duplication in creating and managing multiple function definitions. The transcript illustrates the manual creation and management of multiple function definitions for different data types, leading to code duplication and complexity.', 'Emphasizing the goal of software engineering to avoid code duplication for easier management and updates. The chapter underlines the importance of avoiding code duplication in software engineering to simplify management and updates of functions, especially when modifications or changes are required.']}], 'duration': 228.109, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/I-hZkUa9mIs/pics/I-hZkUa9mIs69.jpg', 'highlights': ['Templates in C++ are more powerful than generics in Java or C#, offering greater flexibility and functionality, making them a significant and complex topic in C++ series.', 'When using a template in a function, it creates a blueprint for generating code based on specified parameters, allowing for flexibility in determining the code that gets put into the template.', 'The need to define multiple overloads for print function based on data type (integer, string, float).', 'Templates can be utilized to create functions that handle different types, such as a print function that can be used for printing integers and other data types.', 'Emphasizing the goal of software engineering to avoid code duplication for easier management and updates.']}, {'end': 616.115, 'segs': [{'end': 257.591, 'src': 'embed', 'start': 228.198, 'weight': 0, 'content': [{'end': 231.919, 'text': 'So in this case, in all three print functions, we have to actually adjust the code.', 'start': 228.198, 'duration': 3.721}, {'end': 240.626, 'text': "What if we could just write that code once and then somehow be able to fill in the blank for the type? That's where we can use a template.", 'start': 232.359, 'duration': 8.267}, {'end': 246.715, 'text': "So instead of writing this code this many times, what I'm actually going to do is just convert this into being a template.", 'start': 241.367, 'duration': 5.348}, {'end': 257.591, 'text': 'The way that we do that syntactically is before the return type we write template, then open an Angular bracket type name and then give it a name.', 'start': 247.335, 'duration': 10.256}], 'summary': 'Using a template to avoid rewriting code multiple times and fill in type blanks.', 'duration': 29.393, 'max_score': 228.198, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/I-hZkUa9mIs/pics/I-hZkUa9mIs228198.jpg'}, {'end': 339.48, 'src': 'embed', 'start': 307.828, 'weight': 3, 'content': [{'end': 314.131, 'text': "In this case, we've chosen to use type name as the type of template parameter, and then T as just a name.", 'start': 307.828, 'duration': 6.303}, {'end': 315.052, 'text': 'So this could be anything.', 'start': 314.151, 'duration': 0.901}, {'end': 318.854, 'text': 'We could call this type, we could call this churner, we could call this anything we really wanted to.', 'start': 315.072, 'duration': 3.782}, {'end': 325.068, 'text': 'And so what happens is this specific variable name, this template kind of argument name.', 'start': 319.764, 'duration': 5.304}, {'end': 330.733, 'text': 'we can use that throughout our actual template code to substitute whatever type in this case gets used.', 'start': 325.068, 'duration': 5.665}, {'end': 339.48, 'text': "So what we've done is, instead of just writing into value like that where we've written T value, which means that when we actually call this code,", 'start': 331.033, 'duration': 8.447}], 'summary': 'Using type name as the template parameter allows flexibility in substituting any type throughout the template code.', 'duration': 31.652, 'max_score': 307.828, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/I-hZkUa9mIs/pics/I-hZkUa9mIs307828.jpg'}, {'end': 394.712, 'src': 'heatmap', 'start': 364.451, 'weight': 0.772, 'content': [{'end': 368.493, 'text': "So what we've done here is we've called the print function with the type int.", 'start': 364.451, 'duration': 4.042}, {'end': 372.496, 'text': 'This template argument here accepts a type name.', 'start': 369.153, 'duration': 3.343}, {'end': 374.558, 'text': 'Now, you can also write this as class, by the way.', 'start': 372.536, 'duration': 2.022}, {'end': 378.781, 'text': 'It is exactly the same, synonymous, right? Class type name.', 'start': 374.958, 'duration': 3.823}, {'end': 385.527, 'text': 'I tend to use type name because class, by just reading the code class kind of implies that it has to be a class type.', 'start': 379.241, 'duration': 6.286}, {'end': 386.887, 'text': "That's not true.", 'start': 386.147, 'duration': 0.74}, {'end': 389.389, 'text': "As you can see, this code still compiles, even though I'm using int.", 'start': 387.128, 'duration': 2.261}, {'end': 394.712, 'text': 'So because of that, I tend to just use type name because it just makes more sense.', 'start': 389.789, 'duration': 4.923}], 'summary': 'Using type name instead of class for template argument, as it implies it has to be a class type.', 'duration': 30.261, 'max_score': 364.451, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/I-hZkUa9mIs/pics/I-hZkUa9mIs364451.jpg'}, {'end': 403.717, 'src': 'embed', 'start': 379.241, 'weight': 4, 'content': [{'end': 385.527, 'text': 'I tend to use type name because class, by just reading the code class kind of implies that it has to be a class type.', 'start': 379.241, 'duration': 6.286}, {'end': 386.887, 'text': "That's not true.", 'start': 386.147, 'duration': 0.74}, {'end': 389.389, 'text': "As you can see, this code still compiles, even though I'm using int.", 'start': 387.128, 'duration': 2.261}, {'end': 394.712, 'text': 'So because of that, I tend to just use type name because it just makes more sense.', 'start': 389.789, 'duration': 4.923}, {'end': 400.535, 'text': 'So we write type name T, which means that the first template argument that we have here is an actual type.', 'start': 394.952, 'duration': 5.583}, {'end': 401.916, 'text': 'So we can write int here.', 'start': 400.735, 'duration': 1.181}, {'end': 403.717, 'text': 'we can write std string.', 'start': 402.296, 'duration': 1.421}], 'summary': 'Using type name instead of class for improved clarity and flexibility.', 'duration': 24.476, 'max_score': 379.241, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/I-hZkUa9mIs/pics/I-hZkUa9mIs379241.jpg'}, {'end': 526.382, 'src': 'embed', 'start': 500.207, 'weight': 2, 'content': [{'end': 505.91, 'text': "And again, that's because the template only gets created when we actually call it because it's just a template.", 'start': 500.207, 'duration': 5.703}, {'end': 506.831, 'text': "It's not actual code.", 'start': 505.93, 'duration': 0.901}, {'end': 514.234, 'text': 'It gets materialized, I guess, into real code that gets sent to the compiler and compiled based on the usage of the template.', 'start': 507.231, 'duration': 7.003}, {'end': 519.537, 'text': "Now, MSVC won't tell you about errors in templates that you're not actually using.", 'start': 514.594, 'duration': 4.943}, {'end': 521.837, 'text': 'Some compilers like Clang actually will.', 'start': 519.876, 'duration': 1.961}, {'end': 523.639, 'text': 'So it is kind of compiler dependent.', 'start': 521.918, 'duration': 1.721}, {'end': 526.382, 'text': "But in this case, you can see we don't actually get any errors.", 'start': 524.099, 'duration': 2.283}], 'summary': 'Template code only creates actual code when called, leading to compiler-dependent error reporting.', 'duration': 26.175, 'max_score': 500.207, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/I-hZkUa9mIs/pics/I-hZkUa9mIs500207.jpg'}, {'end': 625.385, 'src': 'embed', 'start': 598.845, 'weight': 1, 'content': [{'end': 603.967, 'text': "And in fact, that's a lot of what the standard template library actually is in C++.", 'start': 598.845, 'duration': 5.122}, {'end': 606.068, 'text': "It's just completely using templates.", 'start': 604.087, 'duration': 1.981}, {'end': 613.072, 'text': "Let's take a look at an example where we're not actually using a type as a template argument and let's do it for a class instead of for a function.", 'start': 606.509, 'duration': 6.563}, {'end': 614.253, 'text': "Let's just get rid of this print.", 'start': 613.152, 'duration': 1.101}, {'end': 616.115, 'text': "function that we've created over here.", 'start': 614.833, 'duration': 1.282}, {'end': 622.382, 'text': "And let's create a class suppose that I wanted to create an array, but I wanted this array class to actually be created on the stack.", 'start': 616.395, 'duration': 5.987}, {'end': 625.385, 'text': "So what I basically mean is let's just create a class called array.", 'start': 622.562, 'duration': 2.823}], 'summary': 'C++ standard template library heavily uses templates. example of using a class as a template.', 'duration': 26.54, 'max_score': 598.845, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/I-hZkUa9mIs/pics/I-hZkUa9mIs598845.jpg'}], 'start': 228.198, 'title': 'C++ templates', 'summary': 'Explores using templates in code to avoid repetition, explaining c++ template parameters and type inference, and demonstrating automatic type deduction in templates, showcasing their flexibility and power.', 'chapters': [{'end': 270.436, 'start': 228.198, 'title': 'Using templates in code', 'summary': 'Discusses the use of templates in code to avoid repetitive coding by writing the code once and using a template to fill in the type, resulting in the ability to compile the code successfully.', 'duration': 42.238, 'highlights': ['The use of templates in code allows for writing code once and filling in the type, reducing repetitive coding and improving efficiency.', "By using the syntax 'template