背景
我有一个非模板化的类,它声明了一个模板化的成员函数 。
它看起来类似于以下内容:
.HPP
class Foo { private: std::string libFilename; bool libLoaded; bool libFunctionsLoaded; HINSTANCE libHandle; std::map<std::string, long long int> libFunctions; public: Foo(std::string libFilename); ~Foo(void); template<typename FunctionPointerType> FunctionPointerType getFunctionPtr(std::string functionName); void addUnitTestBlock(UnitTestBlock& newGroup); }; template<typename FunctionPointerType> FunctionPointerType Foo::getFunctionPtr(std::string functionName) { return reinterpret_cast<FunctionPointerType>(this->libFunctions[functionName];); }问题
但是,在模板函数内部,map是size = 0.由于某种原因,当涉及到map <>时,模板函数无法看到正确的类成员。 它确实看到了libLoaded和libFunctionsLoaded的正确值。 此外,直到填充了libFunctions映射后才会调用模板函数。
以下是main的结构,只是为了让您了解调用模式:
//DLL Function pointer prototypes typedef int (*TestFunction)(int,char); //Loads the whole DLL and populates the member variables including the map... Foo libraryLoader= Foo("library.dll"); TestFunction function = libraryLoader.getFunctionPtr<TestFunction>("testFunc"); if(function(2, 'a') == 99) { std::cout << "Pass" << std::endl; } else { std::cout << "Fail" << std::endl; }Background
I have a non-templated class which has a templated member function declared.
It looks similar to the following:
.hpp
class Foo { private: std::string libFilename; bool libLoaded; bool libFunctionsLoaded; HINSTANCE libHandle; std::map<std::string, long long int> libFunctions; public: Foo(std::string libFilename); ~Foo(void); template<typename FunctionPointerType> FunctionPointerType getFunctionPtr(std::string functionName); void addUnitTestBlock(UnitTestBlock& newGroup); }; template<typename FunctionPointerType> FunctionPointerType Foo::getFunctionPtr(std::string functionName) { return reinterpret_cast<FunctionPointerType>(this->libFunctions[functionName];); }Problem
However, inside of the template function, the map is size = 0. For some reason the template function cannot see the proper class member when it comes to the map<>. It does see the proper values for libLoaded and libFunctionsLoaded. Additionally, the template function is not called until after the libFunctions map has been populated.
Here is how main is structured, just to give you an idea of the call pattern:
//DLL Function pointer prototypes typedef int (*TestFunction)(int,char); //Loads the whole DLL and populates the member variables including the map... Foo libraryLoader= Foo("library.dll"); TestFunction function = libraryLoader.getFunctionPtr<TestFunction>("testFunc"); if(function(2, 'a') == 99) { std::cout << "Pass" << std::endl; } else { std::cout << "Fail" << std::endl; }This will pass if I get rid of the template and just do the cast.
My goal was to remove the need for casting on the users part, and just have them pass the type of the Function Pointer in and cast it for them. Any ideas on why the template function cannot see a std::map but it can see a bool?
最满意答案
检查您的默认构造函数,复制构造函数以及您编写/重载的任何其他自定义构造函数/ move / operator函数。 确保这些正在做你认为他们正在做的事情。
模板化函数在非模板化类中工作得很好。 数据不准确或丢失很可能是复制问题。
编辑:正如pmr在下面提到的那样......遵守五条规则。 这绝对是重点。
Check your default constructor, copy constructor, and any other custom constructors/move/operator functions you write/overload. Make sure these are doing what you think they are doing.
Templated functions work just fine in non-templated classes. Data being inaccurate or missing is most likely a copy issue.
Edit: As pmr mentions below... Obey the rule of five. That is definitely the main point.
更多推荐
发布评论