3

As some of you may know from my recent posts i am studying for a C++ exam which the content for the class was delivered very poorly. I am basically having to self teach everything myself so bear with me here.

This is an exam question:

(i)Explain the concepts of templates as defined in the C++ language. Be sure to differentiate between what the programmer does and what the compiler does.

My current rationale:

(i) A template allows a function or class to operate using generics. This allows the programmer to effective program X functionality once, and be able to use this functionality with many different data types without having to rewrite the application or parts of the application multiple times.

My problem is i have no idea how the compiler handles the use of templates.

I am unsure what the compiler does at this stage, if somebody could clear this up it would be helpful.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
chris edwards
  • 1,332
  • 4
  • 13
  • 31
  • 2
    "Explain the concepts of templates" -- dat pun. – Shoe May 12 '14 at 21:33
  • 4
    How many months do you get to answer that question? It is rather broad. – juanchopanza May 12 '14 at 21:35
  • 1
    Ask yourself the question "how would I handle it" - the compiler doesn't to much different. Implementations may vary - the standard is not very strict when it comes to the actual implementations. Just keep in mind C++ is statically typed and compiled language - which imposes some limitations on templates. The compiler will not generate a specialization unless you ask it for one and provide every needed "interface". – dtech May 12 '14 at 21:42
  • You're expected to know what compiler is required to accomplish, but not how it actually does so. They're not the same thing. – user207421 May 12 '14 at 21:46

3 Answers3

5

Templates in C++ are implemented through substitution. It's not like Java generics which just type check the code which involves the generics class and then compiles it using raw references (type erasure).

Basically C++ creates a different class/method for each actual template argument used in your code. If you have your

template<typename T>
void myMethod(T t)
{
  //
}

what happens at compile time is that a different method is compiled for each type the template is actually used. If you use it on myMethod(50) and myMethod("foo") then two overloaded version of the method will be available at runtime. Intuitively this means that templates could generate code bloating but in practice the same expressiveness is obtained by a larger codebase without templates with less readability so that's not a real concern.

So there is no black magic behind them (ok there is if you consider meta programming or partial specialization).

Jack
  • 131,802
  • 30
  • 241
  • 343
4

let's say you write a function using templates:

template <typename T>
void function(T t){
 doSomething();
}

for each data type you call this function, the compiler simply replaces the 'T' with that data type, say 'int' and generates code for that like you've written this function with 'int' instead of 'T' since the beginning. This is probably the right (but not the complete) answer if others agreed.

Ahmad Siavosh
  • 646
  • 1
  • 9
  • 22
  • 2
    Not a particularly well-rounded example, considering you don't even use the parameter, nor do you demonstrate how a template may require that T implements certain "interfaces". – dtech May 12 '14 at 22:04
  • @ddriver you're right, but it's enough for understanding the concept. – Ahmad Siavosh May 12 '14 at 22:07
  • I think he'd be happy with hinting at the catches, considering the kind of random garbage most compilers spew out when templates mess up. Simple is good, but the catches must be at least mentioned, no need for details. – dtech May 12 '14 at 22:15
2

For each instance of an object of a different type that you create or in case of functions the different type of arguments that you use, the compiler simply makes an overloaded version at compile time. So if you have a template function like a sort function and use that function for int and double arrays, then the compiler have actually made two functions: one using int and the other using double. This is the simplest explanation I could give. Hope it's useful.

user1
  • 601
  • 1
  • 6
  • 12
  • I think i understand you. Basically the compiler will cover every possible variation of the function that could be present, so it effective handles any generic passed to the function(s). – chris edwards May 12 '14 at 21:35
  • 1
    @chrisedwards No, the compiler generates new code for every variation that *is* present. And don't use the word 'generics' in association with templates. They are completely different things. – user207421 May 12 '14 at 21:38
  • 1
    Yes. Try to make a simple function like `bool greater(T t1, T t2){ return t1 > t2` and simply pass a class of your own which does not handle the operator > . You will see the error that the compiler generates and then you will fully understand the idea that the compiler is simply generating overloaded versions using type T. And of course not every possible variation and just the ones that are used. – user1 May 12 '14 at 21:38