<分区>
免责声明 - 老实说,我不确定如何提出这个问题或提供适当的上下文,如果遇到此问题的人可以推荐我需要提供哪些额外信息来澄清上下文,我将不胜感激。而且如果我需要清理下面的代码部分以使其更清晰,我会在收到评论时这样做!
但不管怎么说 -
我正在尝试使用可变参数模板,但每次我编译我的代码(它在公司代码库上)时,编译器(gcc 4.8.4 - C++11)似乎都会跳过所有可变参数代码部分 -
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)...);
}
调用文件-
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();
}
}
当我编译这个 (gcc 4.8.4) 时,目标文件 TestFactory.o 是空的。如果我在我们的代码库(gcc 4.4.6)之外执行此操作,代码将被编译并输出 -
[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))