0

I have a rather unusual problem... we are tasked with mapping a series of hexadecimal values to function pointers. However, I want to be able to call the function with already predefined parameters. Here is a segment of code that I am trying to get to work:

map<short, void(*)(MyClass, MyClass)> mapping = create_map();


map<short, void(*)(MyClass, MyClass)> create_map()
{
    map<short, void(*)(MyClass, MyClass)>> m;
    m[1] = &function(new MyClass(), new MyClass());
    return m;
}

However, it will only let me put function without parameters into the mapping. How do I store this setup and call it later?

5
  • What is problem. and what environment are you working on. Commented Feb 25, 2012 at 15:54
  • 3
    @rerun: I think the problem is fairly well-explained. I'm not sure why the environment is relevant? Commented Feb 25, 2012 at 15:55
  • Why not just create a class that contains the parameters and the function pointer and store that in the map? Commented Feb 25, 2012 at 15:57
  • 1
    I wanted to know if lambdas or bind were available on his platform. Commented Feb 25, 2012 at 16:05
  • Beware that the design as is leaves the responsibility of releasing the memory to the static function, or you will leak the pointers created in the mapping Commented Feb 25, 2012 at 16:13

2 Answers 2

3

If you are using C++11, You can use std::bind:

void function(MyClass* a, MyClass* b) { ... }

typedef map<short, std::function<void()>> funcmap;

funcmap create_map()
{
    funcmap m;
    m[1] = std::bind(function, new MyClass, new MyClass);
    return m;
}

// use:

funcmap mapping = create_map();
mapping[1]();

If not, you can create a struct that stores the arguments and the function pointer. Then write an operator() which will call the function through the function pointer with the arguments it stored:

struct func {
    typedef void(*fptr)(MyClass*, MyClass*);

    func(fptr f, MyClass* m1, MyClass* m2) : ptr(f), arg1(m1), arg2(m2) { }

    void operator()() {
        ptr(arg1, arg2);
    }

    T ptr;
    MyClass* arg1, arg2;
};

void function(MyClass* a, MyClass* b) { ... }

typedef map<short, func> funcmap;

funcmap create_map()
{
    funcmap m;
    m[1] = func(function, new MyClass, new MyClass);
    return m;
}

// use:

map<short, func> mapping = create_map();
mapping[1]();
Sign up to request clarification or add additional context in comments.

4 Comments

I feel like that shouldn't compile, as the map is expecting a function taking two parameters, but you give is a functor taking zero. I've heard boost::bind is magic, but this use is quite a stretch...
@MooingDuck fixed, I always forget to do that (did it the other day in the chat too)
but this solution cannot store regular function pointers to the type in question. I fear his quest is impossible.
@MooingDuck yes, it is impossible to store a plain function pointer with bound arguments, but I think he just wanted to simulate it (that is, I don't think "a real live function pointer" is part of the requirement)
1

If you are using C++11, you can use lambda functions.

If you are using an older version of C++, then you can use functors.

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.