编译器跳过C++中的可变模板/函数

Compiler skipping over variadic template/function in C++

本文关键字:函数 C++ 编译器      更新时间:2023-10-16

免责声明-老实说,我不知道如何界定这个问题或提供正确的上下文,如果有这个问题经验的人能推荐我需要提供哪些额外信息来澄清上下文,我将不胜感激。如果我需要清理下面的代码部分以使其更清晰,我会在收到评论时这样做!!

但不管怎样,

我正在尝试使用可变代码模板,但每次我编译代码(在公司代码库上)时,编译器(gcc 4.8.4-C++11)似乎都会跳过所有可变代码部分-

测试工厂.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);
};

测试工厂.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))

模板仅在使用时实例化。将定义放在实现中而不在该文件中使用它们将导致没有实例化。然后,当您尝试在其他地方使用它时,它将找不到要实例化的实现,因为它在当前翻译单元看不到的实现文件中。您应该将实现留在标头中。请参阅此问题。

如果有NpBaseTest的定义,并且在一个头中移动所有代码(我假设是TestFactory.h),那么您的示例就可以正常工作。下面是一个如何在示例中使用代码的示例。请注意,按照模板的编写方式,它们只接受指向返回NpBaseTest*的函数的指针。

// main.cpp
#include "TestFactory.h"
#include <iostream>
NpBaseTest* test_callback(int x, int y)
{
std::cout << "Called test_callback(" << x << ", " << y << ")n";
return nullptr;
}
int main()
{
// This instantiates TestFactory::RegisterTest<int, int>
TestFactory::RegisterTest<int, int>("my test", &test_callback);
// This instantiates TestFactory::CreateTest<int, int>
NpBaseTest * result = TestFactory::CreateTest<int, int>("my test", 5, 10);
return 0;
}

我已经为clary明确地编写了模板参数。在您的情况下,编译器将能够推导出这些参数,并且该示例将大大简化。您可以简单地调用不带任何参数的模板化方法,它们将从参数中推导出来。

// main.cpp
#include "TestFactory.h"
#include <iostream>
NpBaseTest* test_callback(int x, int y)
{
std::cout << "Called test_callback(" << x << ", " << y << ")n";
return nullptr;
}
int main()
{
// This instantiates TestFactory::RegisterTest<int, int>
TestFactory::RegisterTest("my test", &test_callback);
// This instantiates TestFactory::CreateTest<int, int>
NpBaseTest * result = TestFactory::CreateTest("my test", 5, 10);
return 0;
}

就是这样。如果尝试这个例子,您会看到TestFactory::RegisterTest<int, int>TestFactory::CreateTest<int, int>的符号现在已经生成。