简体   繁体   中英

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

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. 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.

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<>. 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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