I mean pointers and references were hard at one point, but the gist of them was always understood in clarity. I have always been puzzled and perplexed by any tutorial on templates, from the one on this site to mostly all others. I start reading, but my head drops straight into the desk soon after, I am boggled, and I really can't get a grasp of the concept of their semantic structure, implementation, or application 100%. I realize the basics of the purpose somewhat, but it's the semantic structure and implementation details that I can not get right. I can't seem to just "get it" in my head around how they work, and how they are implemented, what for, etc. I want to be able to know most of C++, but I reached the plateau with templates, to name one! Anyone else here has had issues with getting their head around templates?
Any tips, examples, or clarity segments that may also help future seekers in my position?
PS: Heads up on the name, I am not a troll. I just put the first thing that came to my mind as a username, so please ... no need to man the troll-cannons.
*slow clap for the username, even after reading the postscript*
Think of templates as a circuit board that has a few important chips missing. Depending on what chips you put in the provided slots, you can completely change how the circuit works. By changing the chips you put in, you can easily adapt the board to a variety of different tasks.
same thing happened to me. except it was bools functions and templates. idk if it well help but i think this is the site that finally helped me to understand http://www.cprogramming.com/tutorial/templates.html
I wonder what you were looking up for that to be the first thing that comes to mind. (pun intended)
obviuosly hes an assembly programmer and wrote a source file to organize things, which he dubbed org.asm
Then I'm going to complain to my Spanish teacher because we were taught that dog was Perro and fight was la lucha. Unless I'm making that Spanish when it isn't...assumed it was because of en este being in this from my memory.
I wish TypeTraits was built into the language... Boost.TypeTraits looks like a completely different language. MPL is even worse... it is a language inside of a language. It burns my eyeballs just trying to use it.
We have to make concious effort in including them in our programs ...
If you do this then you might be missing the point of templates. You know those tasks you keep on writing over and over again for different programs? The ones like parsing a line of characters based on some unique criteria or translating a number into binary or hex*? But you can't just make a header file for these tasks because it seems like the data type is always changing right? Sometimes you're parsing an std::string, other times it's a wchar_t array, sometimes the number is a double other times it's a float, maybe negative values are valid in one instance but not another so it's signed versus unsigned* etc. etc. This is where templates become useful. They exist to reduce the number of overloaded functions you have to write in order to perform what is basically the same task.
*: I realize these are poor examples but I can't think of any good ones right now.
but @computerquip, boost.TypeTraits (except for some later additions) are built into the language. Since 2007 for those who implemented TR1, since 2011 for everyone. Unless you're saying they should be implemented as keywords rather than library templates (many of them are in fact compiler intrinsics, but the vendors refused to standardize those, so the library has to provide common interface in any case)
boost.MPL is an easy way to become familiar with how templates are used for type manipulation in practice: people often write their own typelists and metafunctions that operate on them, but it's rarely as easy to follow.
Templates are nice, but they are just fancy macros and nothing more. When you think of them that way, they are pretty damn simple. They are *not* a true generic typesystem (if you think of them that way, you quickly encounter severe limitations).
Generic type inference is very weak, almost non-existent.
You can't write a function to add a list of oranges and a list of apples and make the compiler automatically figure out the result is a list of fruit. Or to add a string to a bitset and let the compiler figure out the result is not a bit set anymore but just a set of items. Except trivial cases like function taking T and returning exactly the same type, the compiler can't really do anything interesting with types.
You can't write generic virtual functions. That's why most of C++ code I see is either pure OOP or pure templated, but not both/mixed.
The compiler does not typecheck templated code on generic level, so the provided typesafety is not a tad better than with C macros.