Template Function signature

 

Once I had a talk with one of my friends regarding templates. In between our talk, (we’re talking about, how compiler handling a template).

For the ease of understanding I compared template with macro, the funniest thing is, both of use were had not that much information about the internal details of template.

I compared “A template object instantiation is something like a macro, that it replaces the template parameter with the type we are specifying”.

Suddenly I had a doubt “If I instantiate a template object of 5 different type, how many function signatures will be there?” I asked for my friends’ help.

We discussed it in “C” style interpretation by compiler, talked about the memory size and performance. But we didn’t reach anywhere.

Today I was just going through the MSDN. Our discussion had flashed in my mind. Anyway I decided to look help from experts. So I posted the same at codeproject.com. (You can see my query here). I got only some funny reply for the same.

I started debugging with a sample code. Yes!!! I have taken the function address to the watch window as well as printed to the screen. The result was, the function address differs for different type object and consistent for same type of objects.

If this happens, definitely there should be different function signature and implementation should reside in memory.

Below I explained the sample code used for testing and it’s out put

// Declared a template class as followstemplate class MyTemplateClass{ public:         void Fxn(T inputval)         {                 printf("nFunction Address%xn",Fxn);         } };

// initialized the objects as follows in the main function.int main(){

	printf("Function address for type int");
	MyTemplateClass objint;
	objint.Fxn(10);

	printf("Function address for type int");
	    MyTemplateClass objint1; // rechecking for same address	objint1.Fxn(100);

	printf("Function address for type float");
	MyTemplateClass objfloat;
	objfloat.Fxn(15.0f);

	printf("Function address for type float");
	MyTemplateClass objfloat1;	// rechecking for same address
	objfloat1.Fxn(150.0f);

	printf("Function address for type short");
	MyTemplateClass objshort;
	objshort.Fxn(20);
	printf("Function address for type double");
	MyTemplateClass objdouble;
	objdouble.Fxn(25.5);

	printf("Function address for type long");
	MyTemplateClass objlong;
	objlong.Fxn(30);

}

The output was something like this

Function address for type int: 401000
Function address for type int: 401000
Function address for type float: 401020
Function address for type float: 401020
Function address for type short: 401040
Function address for type double: 401060
Function address for type long: 401080
 
  • http://codeka.com/blogs Dean Harding

    There’s a linker option called “Allow COMDAT folding” in Visual C++’s linker options which, when enabled, will take identical function definitions and merge them at link time.

    Obviously, it doesn’t work if the code generated for the template functions is actually different for each type (which is probably going to be the case when you’re talking about integral types) but certainly for class types, it should fold most function definitions together.

  • http://ya.ru Nicole

    Good site! I’ll stay reading! Keep improving!