DISCLAIMER - Honestly I am not sure how to frame this question or provide the proper context and would appreciate if people experienced with this problem could recommend what extra information I need to provide to clarify the context. And also if I need to clean up the code section below to make it more legible, I will do that as I receive comments!!
But anyway here goes -
I am trying to work with variadic templates but everytime I compile my code (its on a company codebase) the compiler (gcc 4.8.4 - C++11) seems to skip over all the variadic code section -
TestFactory.cpp
/*
* Use this design since Variadic Templates are not available in C++11
* A MapHolder allows us to create a map of variadic functions.
*/
template <class... Args>
struct MapHolder
{
static std::map<std::string, NpBaseTest*(*)(Args...)> CallbackMap;
};
template <class... Args>
std::map<std::string, NpBaseTest *(*)(Args...)> MapHolder<Args...>::CallbackMap;
class TestFactory
{
public:
template <class... Args>
static void RegisterTest(std::string name, NpBaseTest *(*callback)(Args...));
template <class... Args>
static NpBaseTest *CreateTest(const std::string &name, Args &&... args);
};
TestFactory.cpp
template <class... Args>
void TestFactory::RegisterTest(std::string name, NpBaseTest *(*callback)(Args...))
{
MapHolder<Args...>::CallbackMap[name] = callback;
}
template <class... Args>
NpBaseTest *TestFactory::CreateTest(const std::string &name, Args &&... args)
{
return (MapHolder<Args...>::CallbackMap[name])(std::forward<Args>(args)...);
}
Calling file -
void np_test_mgr_print()
{
const char *s = "cavpkotest";
std::string str(s);
TestFactory::RegisterTest(str.c_str(), &CavPkoTest::create);
NpBaseTest *o1{TestFactory::CreateTest<uint16_t>(str.c_str(), 1)};
/* Irrelevant code section */
NpTestMgr::get_instance().insert(o1);
NpTestMgr::get_instance().submit();
}
}
When I compile this (gcc 4.8.4) the object file TestFactory.o is empty. If I do this outside our codebase (gcc 4.4.6) the code is compiled and outputs -
[common]$ nm TestFactory.o | c++filt $1 | grep CreateTest
34:0000000000401d6a W NpBaseTest* TestFactory::CreateTest<unsigned short>(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned short&&)
[common]$ nm TestFactory.o | c++filt $1 | grep RegisterTest
35:0000000000401d40 W void TestFactory::RegisterTest<unsigned short>(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, NpBaseTest* (*)(unsigned short))