简体   繁体   English

模板成员函数无法正确看到非模板类成员

[英]Template Member function cannot see non-templated class member correctly

Background 背景

I have a non-templated class which has a templated member function declared. 我有一个非模板类,其中声明了模板成员函数

It looks similar to the following: 它看起来类似于以下内容:

.hpp .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];);
}

The cpp has implementations to other functions. cpp具有其他功能的实现。 For the sake of making this readable, I am not including it. 为了使它易于阅读,我不包括在内。 Basically, this implementation will load a bunch of dll functions from a library and put the names and the addresses (gathered from GetProcAddress()) into the map you see in class Foo. 基本上,此实现将从库中加载一堆dll函数,并将名称和地址(从GetProcAddress()收集)放入您在类Foo中看到的映射中。

This code has been verified and I can pull out an address and cast it to a function pointer and use the function just fine... 此代码已通过验证,我可以提取地址并将其转换为函数指针,然后就可以正常使用该函数了...

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<>. 但是,在模板函数内部,映射的大小为=0。由于某些原因,当涉及map <>时,模板函数无法看到适当的类成员。 It does see the proper values for libLoaded and libFunctionsLoaded. 它确实看到了libLoaded和libFunctionsLoaded的正确值。 Additionally, the template function is not called until after the libFunctions map has been populated. 此外,直到填充libFunctions映射之后,才调用模板函数。

Here is how main is structured, just to give you an idea of the call pattern: 这是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;
    }

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? 关于模板函数为何看不到std :: map但可以看到bool的任何想法?

Check your default constructor, copy constructor, and any other custom constructors/move/operator functions you write/overload. 检查您的默认构造函数,复制构造函数以及您编写/重载的任何其他自定义构造函数/ move / operator函数。 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. 编辑:正如pmr在下面提到的...服从于5的规则。 That is definitely the main point. 这绝对是重点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM