Online Book Reader

Home Category

Professional C__ - Marc Gregoire [363]

By Root 1528 0
calling ::type will return some valid type. It is not important to know what exacty this returned type is; it’s only important to know that it will return some valid type. However, when the argument to enable_if is false, the result will not have a wrapped type so calling ::type will fail. This is where SFINAE comes into play.

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::value will return true, which will cause enable_if::type to return some valid type. With this instantiation, everything is fine and the compiler can use that version of check_type().

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::value will return false. Because of this, enable_if will not have a wrapped type and calling enable_if::type will fail. The compiler will notice this error but will not yet generate a real compilation error because of SFINAE. The compiler will gracefully backtrack and try to find another check_type() function. In this case the second check_type() overload will work out perfectly because !is_same::value will be true and thus enable_if::type will be some valid type.

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


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.

Chapter 21

Effective Memory Management


WHAT’S IN THIS CHAPTER?

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

Return Main Page Previous Page Next Page

®Online Book Reader