4 Replies - 10064 Views - Last Post: 18 March 2013 - 07:03 AM

#1 CoolTech  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-February 13

Template implementation

Posted 21 February 2013 - 03:27 PM

How the templates are implemented internally by the C++ compiler?
As template is nothing but a genric routing which can be applied on any number of data types.
I know complier does this at compile time but I want to know how exactly the compiled code will be having i.e. increase the size of complied code by having different different fuctions with respect to number of calls to the template for different different data types etc. Basically I would like to know internal implemenationof template not theorytically which I know

Is This A Good Question/Topic? 0
  • +

Replies To: Template implementation

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Template implementation

Posted 23 February 2013 - 06:24 PM

It's basically like a fancy macro. After the compiler goes though all the rules to decide which overload/specialization to use(which is in my opinion the most interesting part) it just plugs it all in and creates a new function. There is interestingly nothing in the standard that says it has to be done this way but it's really the only sensible way to implement it mostly because of the rules concerning function pointers, statics, and some other things; Hence every implementation I've ever heard of creates a different binary function for each type.

Java and C# implement generics differently from C++'s templates; they use polymorphism so that the same object code can be used for multiple types.

GHC(a haskell compiler) actually passes function pointers to achieve parametric polymorphism(the big thing templates allow you to do) as extra parameters but JHC does something more like C++

This post has been edited by ishkabible: 24 February 2013 - 10:02 AM

Was This Post Helpful? 1
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,158
  • Joined: 05-May 12

Re: Template implementation

Posted 26 February 2013 - 07:30 AM

If I recall correctly, versions of the Microsoft compiler from VS2008 and later support template code folding. Templates which have the same number of parameters and whose types are "binary compatible" with each other end up using the same functions. For example, Foo<int> and Foo<someEnum> could end up sharing the same binary implementation. It makes looking at callstacks a little confusing at times because I'll be going: I only used Foo<int> in my class, but why does my callstack show Foo<someEnum> ?
Was This Post Helpful? 2
  • +
  • -

#4 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Template implementation

Posted 26 February 2013 - 10:03 AM

Interesting! I can think of at least 1 case that would come up a lot for me - vector iterators. Sense they are binary compatible with pointers I suppose I've actually written quite a bit of code that would've been folded like that.

It seems like a really rare case however. Generally if I created a new type I don't expect it to behave the same way...but then again I haven't really looked into the binary compatibly of types that I use. Either way I didn't realize that any compiler did that; that's actually really interesting!

I wonder how it handles this case however

if(reinterpret_cast<void*>(&func<Foo<int>>) != reinterpret_cast<void*>(&func<Foo<someEnum>>)) {
    std::cout << "should this print or not?";
} 



my assumption is that if it found this code it would actually have to include both functions and then it could call either one in either case depending on locality. O and in this case it may statically determine that this should be false but in more complex cases of the same situation I don't believe it could, say if you passed it pointers to a function in another object file.

This post has been edited by ishkabible: 26 February 2013 - 10:08 AM

Was This Post Helpful? 0
  • +
  • -

#5 vividexstance  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 665
  • View blog
  • Posts: 2,296
  • Joined: 31-December 10

Re: Template implementation

Posted 18 March 2013 - 07:03 AM

Another thing about template instantiation for classes, is that when you instantiate a template class, and only use some member functions but not all the member functions, only the member functions you use will be defined for that template instantiation.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1