what is a template from c++ intent?

Started by
10 comments, last by frob 9 years, 8 months ago

std::vector _is_ a template. That's why you use it like:

std::vector<int> foo;The `<int>` bit is a clear sign that you're instantiating the `std::vector` class template over the `int` type parameter.

Yes, you are right, I have never noticed std::vector can interleave objects, not only pointers. I avoided std:: lib always and its vectors.

I have coded a pooled memory manager. And I wonder whaether I should do it with a template or with just sizeof operator as I currently have. My point is wheather the template - if not needed- does have a runtime performance impact, or no runtime impact at all.

Advertisement

Yes, you are right, I have never noticed std::vector can interleave objects, not only pointers. I avoided std:: lib always and its vectors.

I have coded a pooled memory manager. And I wonder whaether I should do it with a template or with just sizeof operator as I currently have. My point is wheather the template - if not needed- does have a runtime performance impact, or no runtime impact at all.

No, there is no additional cost to templates.

In fact the opposite is frequently true.

Template functions are typically defined in headers and the compiler nearly always places them in line, eliminating the function overhead. Usually the compiler is then able to further optimize the code to eliminate load/store patterns, move invariants from loops, or otherwise speed things up. Many of those deeply nested template functions can be eliminated when layer after layer is removed through inline optimizations, very often reducing what would be very slow in debug builds to a very simple set of release build code. As an example, some of the built in containers will go through ten or more indirections with assorted tests in debug mode, but in release mode is optimized away completely resulting in just a single direct memory addressing. Algorithm libraries can sometimes suck in large chunks of raw code resulting on many optimization opportunities.

Lambda functions similarly offer an optimization opportunity to the compiler. In the worst case it devolves to a function call, but in the best case everything in the function can be elided or inlined.

Those are just two of many examples of how the C++ compilation model can be leveraged to produce even faster, more efficient code. Of course there is a cost to be paid when it comes to compilation. The cost to pull in all those headers, to inline everything, to evaluate hundreds or even of thousands of potential template substitutions is one of the reasons people dislike the language -- all these tasks increase build times. But the end result is an executable that is heavily optimized and CAN BE extremely difficult to beat in terms of performance.

There are exactly two C++ features that have a performance cost beyond their direct usage. Those two features are C++ exceptions and RTTI. All commercial games I have worked on or studied have disabled both of these features. Both of these were heavily debated and nearly removed from the original C++ standard and their use remains a contentious issue to this day. Everything else you can use and feel comfortable knowing that you are using a library that is well defined, debugged, and fast.

There are some good reasons to use alternatives to the standard C++ libraries. It is not because of a fear of templates, but because the functionality the standard library offers can be improved on in some measurable way. One oft-cited article in the C++ standard working group is the fairly old writeup of EA's Standard Template Library. It evaluates several things in the 2003 standard that had somewhat better solutions, along with metrics about why the alternative form (which also relied heavily on templates) produced somewhat better results on a range of platforms. Note that those who wrote it did not fear the standard libraries and the language functionality. Instead they embraced it and identified ways to make the system even better by leveraging templates and other features in slightly different ways. Then they ran side-by-side comparisons, improving it further. I liked how in the timing comparisons they also call out the few cases where the library is less performant than the system's library, and how they are usually offset by dramatic performance improvements in other areas.

As for your not noticing the templates, even in the original 1998 C++ standard the processing of templates and libraries using them represented about 2/3 of the language standard. If you actually learn C++ (instead of learning book about C++) then templates are impossible to miss.

If you don't use templates in your code, you are not using C++. Instead you are likely using "C with classes".

This topic is closed to new replies.

Advertisement