编译器跳过C++中的可变模板/函数
Compiler skipping over variadic template/function in C++
免责声明-老实说,我不知道如何界定这个问题或提供正确的上下文,如果有这个问题经验的人能推荐我需要提供哪些额外信息来澄清上下文,我将不胜感激。如果我需要清理下面的代码部分以使其更清晰,我会在收到评论时这样做!!
但不管怎样,
我正在尝试使用可变代码模板,但每次我编译代码(在公司代码库上)时,编译器(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>
的符号现在已经生成。
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用不带参数的函数访问结构元素
- 代码在main()中运行,但在函数中出现错误
- 内置函数可查看CPP中的成员变量
- 如何获取std::result_of函数的返回类型
- 如何在c++中为模板函数实例创建快捷方式
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗