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 ', the code can be converted into a template, enabling the use of T instead of the actual type, such as void print T value.", 'Compiling the code with the template syntax results in successful compilation, as demonstrated by hitting Ctrl F7.']}, {'end': 439.663, 'start': 270.456, 'title': 'C++ template parameters and type inference', 'summary': 'Explains how c++ template parameters and type inference work, with examples of using type name as a template parameter, demonstrating the ability to specify types and substitute them within the template code, and showing the implicit type inference by c++ based on the arguments used.', 'duration': 169.207, 'highlights': ['The chapter explains how C++ template parameters and type inference work The transcript delves into the concept of C++ template parameters and type inference, providing insights into the inner workings of these mechanisms.', 'Examples of using type name as a template parameter The speaker demonstrates the usage of type name as a template parameter, emphasizing its flexibility and applicability in various scenarios.', 'Demonstrating the ability to specify types and substitute them within the template code The speaker illustrates the process of specifying types and substituting them within the template code, showcasing the practical application of template parameters.', "Showing the implicit type inference by C++ based on the arguments used The transcript highlights the implicit type inference capability of C++ based on the arguments used, elucidating the language's ability to deduce types without explicit declarations."]}, {'end': 616.115, 'start': 440.203, 'title': 'Template in c++: automatic type deduction', 'summary': "Explains how templates in c++ allow automatic deduction of types, with examples demonstrating that the template doesn't actually exist until it is called, and how different versions of templates are created based on the arguments used, illustrating the flexibility and power of templates in c++.", 'duration': 175.912, 'highlights': ['Templates in C++ allow automatic deduction of types, making it unnecessary to specify the type, simplifying the coding process. The automatic deduction of types in templates eliminates the need to specify the type, simplifying the coding process.', "The template doesn't exist until it is called, and different versions of the template are created based on the arguments used, demonstrating the flexibility and power of templates in C++. The template only gets created when it is called, and different versions are created based on the arguments used, showcasing the flexibility and power of templates in C++.", 'Templates in C++ are not limited to types or functions; entire classes can be created based on templates, as demonstrated in the standard template library. Templates in C++ are not limited to types or functions and can be used to create entire classes, as seen in the standard template library.']}], 'duration': 387.917, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/I-hZkUa9mIs/pics/I-hZkUa9mIs228198.jpg', 'highlights': ['The use of templates in code allows for writing code once and filling in the type, reducing repetitive coding and improving efficiency.', 'Templates in C++ allow automatic deduction of types, making it unnecessary to specify the type, simplifying the coding process.', 'The template only gets created when it is called, and different versions are created based on the arguments used, showcasing the flexibility and power of templates in C++.', 'Examples of using type name as a template parameter The speaker demonstrates the usage of type name as a template parameter, emphasizing its flexibility and applicability in various scenarios.', 'The chapter explains how C++ template parameters and type inference work The transcript delves into the concept of C++ template parameters and type inference, providing insights into the inner workings of these mechanisms.', 'Templates in C++ are not limited to types or functions and can be used to create entire classes, as seen in the standard template library.']}, {'end': 1060.533, 'segs': [{'end': 750.701, 'src': 'heatmap', 'start': 635.416, 'weight': 1, 'content': [{'end': 642.4, 'text': "Now I can't just kind of put in a variable size or anything like that, because since this is a stack allocated array,", 'start': 635.416, 'duration': 6.984}, {'end': 644.402, 'text': 'it actually has to be known at compile time.', 'start': 642.4, 'duration': 2.002}, {'end': 647.824, 'text': 'Now obviously we could use allocA or something like that to grow the stack dynamically.', 'start': 644.442, 'duration': 3.382}, {'end': 650.245, 'text': "We're not going to use any of that in this example.", 'start': 648.324, 'duration': 1.921}, {'end': 655.428, 'text': 'We just want to basically create a normal kind of C style array on the stack.', 'start': 650.325, 'duration': 5.103}, {'end': 658.19, 'text': 'So this size value does in fact have to be known at compile time.', 'start': 655.588, 'duration': 2.602}, {'end': 661.734, 'text': 'Now, templates are evaluated at compile time, so this is perfect.', 'start': 658.55, 'duration': 3.184}, {'end': 664.737, 'text': 'What I can do is convert this class into being a template.', 'start': 662.134, 'duration': 2.603}, {'end': 671.525, 'text': 'But instead of using type name as my template argument, I can actually use just an integer right?', 'start': 665.078, 'duration': 6.447}, {'end': 674.669, 'text': "And I'll call this n, which basically just stands for number.", 'start': 671.846, 'duration': 2.823}, {'end': 676.811, 'text': "And then instead of size over here, I'm going to write n.", 'start': 674.809, 'duration': 2.002}, {'end': 682.198, 'text': "And then finally, I'll just add one public function to this array, which is just going to return the size of the array.", 'start': 677.532, 'duration': 4.666}, {'end': 686.043, 'text': "So I'll write int get size and then I'll just return n okay?", 'start': 682.218, 'duration': 3.825}, {'end': 691.17, 'text': "So what's going to happen here is exactly the same thing that actually happened with our function.", 'start': 686.504, 'duration': 4.666}, {'end': 701.639, 'text': "When we call this array and we specify a size like five, and I'll just call this array, what happens is this code gets compiled based on its usage.", 'start': 691.55, 'duration': 10.089}, {'end': 709.182, 'text': 'So, in this case, five is the template argument, which means that a version of this class gets created which replaces n with five,', 'start': 701.679, 'duration': 7.503}, {'end': 710.383, 'text': 'like that and like that.', 'start': 709.182, 'duration': 1.201}, {'end': 712.543, 'text': 'So this is actually what we end up with.', 'start': 710.503, 'duration': 2.04}, {'end': 714.484, 'text': 'This is the code that we end up with.', 'start': 712.624, 'duration': 1.86}, {'end': 719.447, 'text': 'And then of course, if I call array.getSize here, and print that to the console.', 'start': 714.644, 'duration': 4.803}, {'end': 721.389, 'text': "I'll get rid of this extra copy I've created here.", 'start': 719.507, 'duration': 1.882}, {'end': 722.29, 'text': "That's my code.", 'start': 721.63, 'duration': 0.66}, {'end': 723.171, 'text': "Let's hit F5.", 'start': 722.33, 'duration': 0.841}, {'end': 724.853, 'text': 'You can see that I get five printing.', 'start': 723.371, 'duration': 1.482}, {'end': 725.934, 'text': "So that's how it works.", 'start': 724.933, 'duration': 1.001}, {'end': 727.996, 'text': "So you can see that we don't just have to use types.", 'start': 726.154, 'duration': 1.842}, {'end': 735.064, 'text': 'We can even use integers or other data types to basically specify how we want to generate a class, an entire class in this case.', 'start': 728.036, 'duration': 7.028}, {'end': 736.445, 'text': "Let's go one step further.", 'start': 735.464, 'duration': 0.981}, {'end': 742.091, 'text': 'Suppose that instead of this being an int explicitly, I also wanted to make the type actually variable.', 'start': 736.926, 'duration': 5.165}, {'end': 747.097, 'text': 'So I wanted to be able to specify what type this array actually contains at compile time.', 'start': 742.172, 'duration': 4.925}, {'end': 749.079, 'text': 'Well, I can add another template argument.', 'start': 747.477, 'duration': 1.602}, {'end': 750.701, 'text': "So let's add this one before the number.", 'start': 749.099, 'duration': 1.602}], 'summary': 'Using templates in c++ to create stack-allocated arrays with compile-time known size and type, demonstrating the generation of a class based on template arguments.', 'duration': 115.285, 'max_score': 635.416, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/I-hZkUa9mIs/pics/I-hZkUa9mIs635416.jpg'}, {'end': 815.505, 'src': 'heatmap', 'start': 772.691, 'weight': 0, 'content': [{'end': 777.492, 'text': 'And I can obviously change this to be anything I want, like seString and 50 or whatever.', 'start': 772.691, 'duration': 4.801}, {'end': 783.434, 'text': "And you can see that what I've created is kind of a class that gets automatically created every time I actually use this array.", 'start': 777.592, 'duration': 5.842}, {'end': 784.394, 'text': "So that's pretty cool.", 'start': 783.634, 'duration': 0.76}, {'end': 790.316, 'text': "And what we've actually created here is very similar to how the standard array class actually works in the standard template library.", 'start': 784.454, 'duration': 5.862}, {'end': 792.237, 'text': "that's in C++ right?", 'start': 790.836, 'duration': 1.401}, {'end': 797.879, 'text': "It just has two template arguments, type and size, and it creates an array very similar to the way that we've actually done it here.", 'start': 792.317, 'duration': 5.562}, {'end': 804.881, 'text': 'Now this is a sort of meta programming in C++, because you can see, instead of actually programming what our code does at runtime,', 'start': 798.279, 'duration': 6.602}, {'end': 809.043, 'text': "we're kind of programming what the compiler will actually do during compile time.", 'start': 804.881, 'duration': 4.162}, {'end': 811.544, 'text': 'And this is extremely powerful.', 'start': 809.443, 'duration': 2.101}, {'end': 815.505, 'text': "Now what we're doing here, I think this is a pretty good kind of gentle introduction.", 'start': 811.604, 'duration': 3.901}], 'summary': 'Creating a class in c++ that mimics the standard array class, demonstrating meta programming and its power.', 'duration': 31.051, 'max_score': 772.691, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/I-hZkUa9mIs/pics/I-hZkUa9mIs772691.jpg'}, {'end': 960.935, 'src': 'embed', 'start': 906.349, 'weight': 3, 'content': [{'end': 913.872, 'text': "That might be going a little bit too far, because there's a point where the templates become so complex that if there's an issue with it,", 'start': 906.349, 'duration': 7.523}, {'end': 915.712, 'text': "no one can understand what's going on.", 'start': 913.872, 'duration': 1.84}, {'end': 922.875, 'text': 'And you literally have to just get like a notebook, get a piece of paper and start filling in all these types manually.', 'start': 916.113, 'duration': 6.762}, {'end': 927.197, 'text': 'Of course, you can get the compiler to actually tell you what code is generating and all of that.', 'start': 922.895, 'duration': 4.302}, {'end': 932.099, 'text': 'But even so, like at work,', 'start': 928.177, 'duration': 3.922}, {'end': 940.543, 'text': "we've had cases where we've just had to sit down and spend three hours figuring out what code is actually being compiled and what's going on with our templates and why they're not working.", 'start': 932.099, 'duration': 8.444}, {'end': 943.605, 'text': "When that happens, I think you've gone too far.", 'start': 941.024, 'duration': 2.581}, {'end': 948.067, 'text': "And I don't think that you should get too crazy with templates, even though you can.", 'start': 944.125, 'duration': 3.942}, {'end': 960.935, 'text': 'There is a point where doing things manually and writing out code yourself is actually going to benefit you and your team a lot more than trying to create this massive template magic that just does everything for you but is kind of fragile and very difficult to understand.', 'start': 948.408, 'duration': 12.527}], 'summary': 'Complex templates led to 3-hour debugging, favor manual code over complex templates.', 'duration': 54.586, 'max_score': 906.349, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/I-hZkUa9mIs/pics/I-hZkUa9mIs906349.jpg'}, {'end': 1002.063, 'src': 'embed', 'start': 975.326, 'weight': 6, 'content': [{'end': 983.236, 'text': "Once we actually get into kind of the game engine series as well, we'll start to see real examples of where we might want to use templates.", 'start': 975.326, 'duration': 7.91}, {'end': 985.204, 'text': 'Off the top of my head.', 'start': 984.443, 'duration': 0.761}, {'end': 995.776, 'text': 'I think two kind of really important places where I would definitely use templates are things like logging systems and also even like a material system like for rendering graphics right?', 'start': 985.204, 'duration': 10.572}, {'end': 1002.063, 'text': 'When you have a uniform buffer that can contain various different types templating, that, to an extent, can be extremely useful.', 'start': 995.796, 'duration': 6.267}], 'summary': 'Templates are useful in logging systems and material systems for rendering graphics, with potential for extreme usefulness in uniform buffers.', 'duration': 26.737, 'max_score': 975.326, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/I-hZkUa9mIs/pics/I-hZkUa9mIs975326.jpg'}], 'start': 616.395, 'title': 'Stack-allocated arrays with templates', 'summary': 'Explains how to create a stack-allocated array using templates in c++, showcasing the use of template arguments for type and size, emphasizing the power and flexibility of templates. it also discusses the benefits and risks of using templates, highlighting the potential complexity and challenges, with examples emphasizing the importance of code readability and debugging.', 'chapters': [{'end': 859.599, 'start': 616.395, 'title': 'Stack-allocated array with templates', 'summary': 'Explains how to create a stack-allocated array using templates in c++, showcasing the use of template arguments for both type and size, and emphasizes the power and flexibility of templates for meta programming at compile time.', 'duration': 243.204, 'highlights': ['Creating a stack-allocated array using templates in C++ The chapter discusses the creation of a stack-allocated array class using templates, showcasing the use of template arguments for both type and size.', 'Use of template arguments for both type and size The chapter highlights the use of template arguments for specifying the type and size of the stack-allocated array, demonstrating the flexibility and power of templates for compile-time meta programming.', 'Emphasizing the power and flexibility of templates for meta programming at compile time The chapter emphasizes the significant capabilities of templates for meta programming at compile time, showcasing the potential for creating classes based on template arguments and the dynamic nature of class generation.']}, {'end': 1060.533, 'start': 859.599, 'title': 'Benefits and caution of using templates', 'summary': 'Discusses the benefits and risks of using templates, emphasizing that while templates can automate code generation, overly complex templates can lead to code that is difficult to understand and debug, as highlighted by examples of spending hours deciphering template-generated code.', 'duration': 200.934, 'highlights': ['Overly complex templates can lead to spending three hours figuring out template-generated code, highlighting the potential pitfalls of using templates.', 'Templates can automate code generation based on rules, but overly complex templates can result in code that is difficult to understand and debug.', 'Using templates for logging systems and material systems like rendering graphics can be extremely useful due to their ability to handle various different types in a uniform buffer.', 'The chapter emphasizes the importance of not going too far with templates, as overly complex templates can become fragile and very difficult to understand, leading to challenges in debugging and maintaining code.', 'The transcript discusses the benefits and risks of using templates, emphasizing the need to balance the automation of code generation with the maintainability and understandability of the code.']}], 'duration': 444.138, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/I-hZkUa9mIs/pics/I-hZkUa9mIs616395.jpg', 'highlights': ['Emphasizing the power and flexibility of templates for meta programming at compile time The chapter emphasizes the significant capabilities of templates for meta programming at compile time, showcasing the potential for creating classes based on template arguments and the dynamic nature of class generation.', 'Use of template arguments for both type and size The chapter highlights the use of template arguments for specifying the type and size of the stack-allocated array, demonstrating the flexibility and power of templates for compile-time meta programming.', 'Creating a stack-allocated array using templates in C++ The chapter discusses the creation of a stack-allocated array class using templates, showcasing the use of template arguments for both type and size.', 'The chapter emphasizes the importance of not going too far with templates, as overly complex templates can become fragile and very difficult to understand, leading to challenges in debugging and maintaining code.', 'The chapter discusses the benefits and risks of using templates, emphasizing the need to balance the automation of code generation with the maintainability and understandability of the code.', 'Templates can automate code generation based on rules, but overly complex templates can result in code that is difficult to understand and debug.', 'Using templates for logging systems and material systems like rendering graphics can be extremely useful due to their ability to handle various different types in a uniform buffer.', 'Overly complex templates can lead to spending three hours figuring out template-generated code, highlighting the potential pitfalls of using templates.']}], '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 use of templates in code allows for writing code once and filling in the type, reducing repetitive coding and improving efficiency.', 'Emphasizing the power and flexibility of templates for meta programming at compile time The chapter emphasizes the significant capabilities of templates for meta programming at compile time, showcasing the potential for creating classes based on template arguments and the dynamic nature of class generation.']}