58: C++ Templates. Types And Values.

Take Up Code - Un pódcast de Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes

Categorías:

Both C++ templates and C# generics serve a similar purpose. But where C# uses constraints to enable generics, C++ instead uses the compiler to enable templates. And C++ includes the ability to create templates base on values which will enable you to do things unheard of in C#. Most C# developers don’t even know what they’re missing. When you declare a generic class in C#, you need to specify constraints so that the compiler can figure out if the types that you’re trying to use with the generic class actually meet the requirements or not. If you forget a constraint, then it’s possible to compile code that won’t run. And if you add too many constraints, then it’s possible to generate unnecessary compile errors. C++ doesn’t use constraint. Instead it looks at how the types are actually used and makes sure that a type has all the methods needed by the template. So if you want to use a class as a template parameter, then just look at how the type T, or whatever it’s called in the template, is actually used and make sure that your class would be able to fit. That’s all there is to it. The compiler will make sure that your template parameters meet the needs of the template class. You can also specify a value for your template parameters in C++. This allows you to create distinct types that are slightly different. Maybe you want a door that always opens in vs. another door that always opens out. Instead of creating two door classes, an inDoor and an outDoor, just create a single template door class that takes a bool value to select the direction. The compiler will keep your door class as its own type and make sure that it’s a different type than a door class. Listen to the full episode or you can also read the full transcript below. Transcript If you haven’t already, make sure to listen to the previous episode #57 first because this episode builds on that episode. Many of the core concepts of C# generics apply to C++ templates but with some difference that this episode will explain. I mentioned in the intro that C++ relies more on the compiler than C#. How does this work? I actually find C++ to be a more natural and direct way to express a template. First of all C++ does not have a concept of a base class that’s common to all other classes. Some languages have this idea but not C++. You can create a C++ class that is completely unrelated to any other class if you want. Interfaces also play a huge role in C# generics. And while C++ does have pure virtual abstract classes that serve a similar purpose, you’re not limited to working with interfaces if you don’t want to. One of the problems that C# encountered that I explained in the previous episode was knowing what methods and properties were safe to call from within a generic class method. C# uses constraints to make sure that you can’t create a generic class with a type that fails to meet the constraint. C++ takes a different approach. It checks during compile time if you try to create an instance of a template class if the type you’re using will match the needs of the template. What do I mean by this? Let’s take the adventure game again and start with the classes shop which has a trade method, a tradable class that defines a pure virtual method to get the original value, an inventoryItem class which inherits from tradable and implements the method to get the original value, and a service class which also inherits from tradable and implements the method to get the original value. So far, the only thing that’s really changed is that the tradable interface has become a pure virtual abstract class. You just write the shop class trade method to accept a type T just like before and go ahead and call the original value method on T when you need to. When some code wants to create a shop of inventory items what happens is that the compiler makes sure right then that the template code wi

Visit the podcast's native language site