1

Let's suppose I have the following template function in a.h file

#include <iostream>

template <class T>
void foo(T arg)
{
   ...some code...
   std::cout<< arg <<std::endl;
}

And the following code in a.cpp

#include "Header.h"
#include <string>
int main(int argc, char* argv[])
{
    foo<int>(5);
    foo<double>(5.25);
    foo<std::string>("Hello World");
    return 0;
}

When I look into .map file generated by linker (set /MAP (Generate Mapfile) option in VS) for this executable I see only one entry for foo function, which is

0001:000006f0       ??$foo@H@@YAXH@Z           004016f0 f i main.obj

However my assumption was that it should have contained 3 separate entries for foo function (one per instantiation). Viewing map file with Amap shows that this entry corresponds to

 void foo<int>(int) 

function. Can someone please explain why there are no entries for

void foo<std::string>(std::string) 
void foo<double>(double)

functions?

Thank you

  • Did you actually *use* those instances. If not, the compiler would be well within its rights to optimize them out. – Jesper Juhl Sep 16 '17 at 18:38
  • What do you mean by use ? As you can see in the code of a.cpp I call those functions. – Grigor Aleksanyan Sep 16 '17 at 18:41
  • The compiler might inline the function for some parameters. When I test with VS2017 (without "some code") they are all inlined. – Bo Persson Sep 16 '17 at 20:05
  • Hi guys, looks like inlining was the case. Just looked into .cod files and found that functions were actually linlined. After using __declspec(noinline) I found entries for other instantiations too in the map file. It is strange that compiler inlines functions event when I use VS /Ob0(Disabled) option. I used it before posting this question, wasn't expecting that inlining can still be applied. – Grigor Aleksanyan Sep 16 '17 at 23:29

0 Answers0