• Forum
  • Lounge
  • Am I the only one who has always been pu

 
Am I the only one who has always been puzzled by templates?

Pages: 12
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.
Last edited on
*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.

Has the book Thinking in C++ helped at all? If you haven't read it, then you can get a free and legal electronic download from here (look for volume 1):
http://www.mindviewinc.com/Books/downloads.html

-Albatross
closed account (N36fSL3A)
I had the same problem with templates & pointers.

I soon realized I was over-thinking.

EDIT:
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.
I wonder what you were looking up for that to be the first thing that comes to mind. (pun intended)
Last edited on
closed account (Dy7SLyTq)
I soon realized I was over-thinking

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
ORG
Sets the current origin to a new value. This is used to set the program or register address during assembly.

@Albatross: ¿what does `slow clap' mean?
closed account (N36fSL3A)
@Albatross: ¿what does `slow clap' mean?
Maybe "To slowly clap"?
@Lumpkin: nadie te dió vela en este entierro
closed account (N36fSL3A)
GoogleTranslate wrote:
nobody gave you dog in this fight
Huh?
I hear google translate provides perfect translations and takes into account the semantic differences between languages.
I laugh because other translators give you this:

nobody gave you sail(candle) in this burial(funeral)
The fuq? It specifically translates "vela en este entierro" to "dog in this fight". If you put a period in the middle it translates the individual words correctly.
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.
Coming to the thread, yes even I am in the same boat as orgasm.

Though I think it has more to do with practice. We have to make concious effort in including them in our programs to become more comfortable.

Templates imo is a great feature that should be learnt if we want to improve our program designs.
closed account (S6k9GNh0)
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.
abhishekm71 said:
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.
closed account (Dy7SLyTq)
Another good examle: max of two data types
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.
I've not really done too much with templates. Though, I really should, makes function overloading so much more easier once you get the hang of them. Among other things.

I would rather do:
1
2
3
4
5
6
template <class T>
T getMax(T a, T b){
      T result;
      result = (a > b) ? a : b;
      return (result);
}

Rather than doing:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int getMax(int a, int b){
      int result;
      result = (a > b) ? a : b;
      return (result);
}

float getMax(float a, float b){
      float result;
      result = (a > b) ? a : b;
      return (result);
}

// etc.
}


[EDIT] Forgot closing code tag.
Last edited on by closed account z6A9GNh0
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).

Limitation 1:
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.

Limitation 2:
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.

Limitation 3:
The compiler does not typecheck templated code on generic level, so the provided typesafety is not a tad better than with C macros.

Pages: 12