Professional C__ - Marc Gregoire [363]
When the compiler starts to compile the first line of the main() function, it tries to find a function check_type() that accepts two integer values. It will find the first check_type() function template overload in the source code and will deduce that it can use an instance of this by making T1 and T2 both integers. It will then try to process the third parameter. Since both types are integers and thus the same, is_same However, when the compiler tries to compile the second line in the main() function, it will again try to find a suitable check_type() function. It starts with the first overload and decides it can use that overload by setting T1 to type integer and T2 to type double. It will then try to process the third parameter. This time, T1 and T2 are different types, which means that is_same As mentioned before, relying on SFINAE can become tricky and complicated. It is recommended to use it judiciously. Conclusion As you have seen in this section, template metaprogramming can be a very powerful tool, but it can also get quite complicated. One problem with template metaprogramming, not yet mentioned before, is that everything happens at compile time so you cannot use a debugger to pinpoint a problem. If you decide to use template metaprogramming in your code, make sure you write good comments to explain exactly what is going on and why you are doing something a certain way. If you don’t properly document your template metaprogramming code, it might be very difficult for someone else to understand your code, and it might even make it difficult for yourself to understand your own code in the future. SUMMARY Chapter 21 Effective Memory Management What the different ways are to use and manage memory What the often perplexing relationship is between arrays and pointers A low-level look at working with memory What smart pointers are and how to use them Solutions to a few memory related problems In many ways, programming in C++ is like driving without a road. Sure, you can go anywhere you want, but there are no lines or traffic lights to keep you from injuring yourself. C++, like the C language, has a hands-off approach towards its programmers. The language assumes that you know what you’re doing. It allows you to do things that are likely to cause
This and the previous chapter show you how to use templates for generic programming and template metaprogramming for compile time computations. We hope that you gained an appreciation for the power and capabilities of these features, and an idea of how you could apply these concepts to your own code. Don’t worry if you didn’t understand all the syntax, or follow all the examples, on your first reading. The concepts can be difficult to grasp when you are first exposed to them, and the syntax is tricky whenever you want to write somewhat complicated templates. When you actually sit down to write a template class or function, you can consult this chapter and the previous one for a reference on the proper syntax.
WHAT’S IN THIS CHAPTER?