2

I need to use a function template which is available in a header file via a function pointer which is available as a struct member.

For example:

File: mytemplate.h

template<typename T> const bool GT(const T& pa_roIN1, const T& pa_roIN2)
{
    bool temp = false;
    if(pa_roIN1 > pa_roIN2){
        temp = true;
    }
    return temp;
}

template<typename T> const bool EQ(const T& pa_roIN1, const T& pa_roIN2)
{
    bool temp = false;
    if(pa_roIN1 == pa_roIN2){
      temp = true;
    }
    return temp;
}

template<typename T> const bool GE(const T& pa_roIN1, const T& pa_roIN2)
{
    bool temp = false;
    if(pa_roIN1 >= pa_roIN2){
      temp = true;
    }
    return temp;
 }

File: mystruct.h:

struct mystruct {
    std::string  funcName;
    bool (*funcPtr)(int,int);
};

typedef std::map<int, mystruct> mystructList;

File: mycpp.cpp:

#include "mytemplate.h"
#include "mystruct.h"

mystruct MyGreaterStruct[3] = {
  {"GREATER_THAN", &GT},
  { "EQUAL", &EQ},
  { "GREATER_N_EQUAL", &GE}
};

mystructList MyList;
int main(void)
{
     int size = sizeof(MyGreaterStruct)/sizeof(mystruct);
     for(int i = 0; i < size; i++) {
       MyList.insert(std::pair<int, mystruct>(i,MyGreaterStruct[i])); }

     for(mystructList::iterator iter = MyList.begin(); iter != MyList.end(); iter++) {
        printf("\nResult of func : %s for values 100,50 : %d",iter->second.funcName.c_Str(),iter->second->funcPtr(100,50));
     }
}

I am not sure this the correct way of doing it. But there is no result that gets printed on the console.

1
  • "There is no result"? You sure the code just doesn't fail to compile? Commented Aug 21, 2015 at 3:12

1 Answer 1

2

The error you get when compiling your code tells you what is wrong:

mycpp.cpp:8:1: error: no matches converting function ‘GT’ to type ‘bool (*)(int, int)’
 };
 ^
mytemplate.h:1:33: note: candidate is: template<class T> const bool GT(const T&, const T&)
 template<typename T> const bool GT(const T& pa_roIN1, const T& pa_roIN2)
                                 ^

There's no way to instantiate that template to match the signature of the function pointer, as the template takes two references as arguments, while the pointer takes two ints. If you can't change either the struct or the templates, and you are using C++11, you could try using lambdas instead:

mystruct MyGreaterStruct[3] = {
  {"GREATER_THAN", [](int a, int b)->bool { return GT(a, b); } },
  { "EQUAL", [](int a, int b)->bool { return EQ(a, b); } },
  { "GREATER_N_EQUAL", [](int a, int b)->bool { return GE(a, b); } }
};

but no matter how you slice it, you need to either change the arguments or use a little function that converts the arguments from one form to the other.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.