1
#include <iostream>
#include <unordered_map>
#include <utility>
#include <typeinfo>

using namespace std;
class Handle{
    public:
    int val;
    bool getAskPrice(int& tmp) const
    {
        tmp = val;
        return true;
    }
    
    bool setAskPrice(int& tmp)
    {
        val = tmp;
        return true;
    }
};

template<class RT, class ARG>
struct convertToAFL{
    static RT to_afl(ARG);
};

template<class RT, class ARG>
struct convertFromAFL{
    static RT from_afl(ARG);
};

template<>
struct convertToAFL<float, int>
{
    static float to_afl(int& value)
    {
        return static_cast<float>(value);
    }
};

template<>
struct convertFromAFL<int, float>
{
    static int from_afl(float& val)
    {
        return static_cast<int>(val);
    }
};

struct Getter{
    template<typename TICK_D, bool (Handle::*Getter)(TICK_D&) const, typename AFL_D>
    static AFL_D getter(const Handle& handle)
    {
        TICK_D temp;
        bool exists;
        exists = (handle.*Getter)(temp);
        AFL_D x = convertToAFL<AFL_D, TICK_D>::to_afl(temp);
        return exists ? x : -1;
    }
};

struct Setter{
    template<typename TICK_D, bool (Handle::*Setter)(TICK_D&), typename AFL_D>
    static void setter(Handle& handle, AFL_D& val)
    {   
        TICK_D x;
        x = convertFromAFL<TICK_D, AFL_D>::from_afl(val);
        (handle.*Setter)(x);
    }
};

int main()
{   
    Handle h;
    float val = 20.0;
    Setter::setter<int, &Handle::setAskPrice, float>(h, val);
    std::cout<<Getter::getter<int, &Handle::getAskPrice, float>(h);
    
    //std::pair<, &Setter::setter<int, &Handle::setAskPrice, float>> x;
    return 0;
}

The above code works as expected, however, in the main() instead of calling the functions, how can I store the pointer to the templatized Setter:setter() and Getter::getter() ? I m trying something like

std::pair<&Setter::setter<int, &Handle::setAskPrice, float>, &Getter::getter<int, &Handle::getAskPrice, float>(h)> func_pair;

And be able to call the functions later.

But i get an error saying

main.cpp: In function ‘int main()’:
main.cpp:85:118: error: type/value mismatch at argument 1 in template parameter list for ‘template struct std::pair’
     std::pair<&Setter::setter<int, &Handle::setAskPrice, float>, &Getter::getter<int, &Handle::getAskPrice, float>(h)> func_pair;
                                                                                                                      ^
main.cpp:85:118: note:   expected a type, got ‘& setter’
main.cpp:85:118: error: template argument 2 is invalid

3 Answers 3

3

Static member functions are just a normal functions. You can store these pointers like this:

std::pair<void (*)(Handle& handle, float& val), float (*)(const Handle& handle)> 
    func_pair(&Setter::setter<int, &Handle::setAskPrice, float>, &Getter::getter<int, &Handle::getAskPrice, float>);
Sign up to request clarification or add additional context in comments.

Comments

1

Your problem is that template parameter is a type, but you are passing a value (pointer) as an argument. Instead, you could use auto like this:

auto func_pair = std::make_pair(&Setter::setter<int, &Handle::setAskPrice, float>, &Getter::getter<int, &Handle::getAskPrice, float>);

Edit: if you are using C++03, std::make_pair() is still available, but not auto. You will need to describe the type manually with a series of typedefs.

Comments

1

You can use decltype to get the pointer type.

Example:

std::pair<int, decltype(&Setter::setter<int, &Handle::setAskPrice, float>)> x = {
    1, &Setter::setter<int, &Handle::setAskPrice, float>
};

Prior to C++11:

std::pair<int, void(*)(Handle&, float&)> x(
    1, &Setter::setter<int, &Handle::setAskPrice, float>
);

2 Comments

I am using C++ 03
@Sujith Ok, I added a version without decltype.

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.