首页 > 解决方案 > Variadic template argument deduction - c++11 - many functions linked to one

问题描述

I'm experiencing some strange occurrences regarding varidaic templates that I never saw before. In order to keep it simple i'll give a simple example of what I was trying to achieve, the real code is a bit more involved.

So I have one function that looks like:

template <typename T, typename F, typename ... Args> 
static T Func( std::string str, Args&& ... args )
{
 ... Do something
}

I then call this function a number of times from different locations, where most times the passed types are different. But when I debug this, when I look for the symbol of Func, I get that the same function address has a number of different symbols:

00000000`70ba4ae0 Func<unsigned long,unsigned long (__stdcall*)(wchar_t const *),wchar_t const *>

00000000`70ba4ae0 Func<void *,void * (__stdcall*)(unsigned int),unsigned int>

00000000`70ba4ae0 Func<int,int (__stdcall*)(void *),void *>

So they are all basically the same function. When I try to call for example:

call Func<void *,void * (__stdcall*)(unsigned int),unsigned int>

In the debugger I see:

call Func<int,int (__stdcall*)(void *),void *>

I can see that there is a generated symbol for every template function instance, but every such instance that has the same number of arguments and the arguments are the same byte-size are just linked to one function.

While I can understand why this may happen, but is there a way to force each function to be standalone?

标签: c++11variadic-templates

解决方案


The issue was indeed COMDAT folding. Turning off the OPT:ICF and OPT:REF flags did the trick. Although worth noting that compiling with VS 2017 with the flags produced more "sensible" results than compiling the same code with VS 2013.


推荐阅读