C++ templates, are they worth it?

Started by
10 comments, last by Ravyne 10 years, 3 months ago


I recently swapped most of my dynamic memory allocations by using vectors (except where it doesn't bring anything).

What do you mean by "Except where it doesn't bring anything"? Why would you use C array in a C++ code? With zero optimizations, I don't believe the performance would be so much different, as std::vector is just a wrapper around C style array? However the functionality you get with STD arrays is totally worth performance.

And to answer your question about templates, of course learn it. Templates can be scary (it was for me); however once you get comfortable with it, you can write codes that are very flexible.

Advertisement

First learn to make good use of templates indirectly, e.g. as someone mentioned, through using vector, map, pair etc. Then once you're very comfortable with that, you might want to start getting involved with them directly i.e. seeing how they are written and then writing your own.

The same advice I give for learning OOP and classes fwiw.

"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

well, templates can partially be replaced by the new "auto" type in c++

well, templates can partially be replaced by the new "auto" type in c++

No, I don't think so. Not in C++11 anyways. The use of the 'auto' keyword can make it so that you don't have to state an explicit template type more than once when once will do, but since auto is not a type itself -- it only infers the type of an expression known to the compiler -- it needs a type on which to act on in the first place, template or otherwise.

In C++14 I believe they've allowed for auto parameters in lambdas for sure (and I think other functions too), and for return types -- and that can be used for something that's vaguely a subset of templates. But its AFAIK just shorthand for actual templates, you can't access variadic template parameters, can't specialize, and you can't even constrain types at all (maybe with traits-lite? I'm not sure). Its very, very limited.

throw table_exception("(? ???)? ? ???");

This topic is closed to new replies.

Advertisement