Std::vector of void function pointers

I’m creating an application that contains a class with a variety of methods that perform animations on objects. Each method has two parameters (int, float). The animations will be triggered randomly, so my idea was to create a std::vector of function pointers, and then randomly pick out methods to call.

Problem is, I can’t get it to compile. I’ve searched online, and most people recommend using C++11’s std::function feature or the Boost library’s boost:function. Well, neither of these seem to be used with openframeworks, and I’m not sure where to turn. Here’s some applicable code:


std::vector<void(*)(int,float)> animationsVector;
void circleAnimation01(int circleGroup, float time);


void polygonCluster01::circleAnimation01(int circleGroup, float animLength)

void polygonCluster01::setupAnimations()
    animationsVector.push_back(circleAnimation01(1, 2.0));  

The above attempt to add an entry to the animationsVector gives this error:

Reference to type ‘const value_type’ (aka ‘void((*const)(int,float)’) could not bind to an rvalue of type ‘void’

If I switch it to:

animationsVector.push_back(&circleAnimation01(1, 2.0));

I get:

Cannot take the address of an rvalue of type ‘void’

I think (I think!) I need to somehow create an object to store in the vector, but I’m not sure how to go about this. Any ideas?


this is a nice one! You can work with c-style function pointers, as you already do. You just have to leave out the arguments when storing the function, since you’re storing the function and not the function with arguments (which would be actually storing the return value of that function…)

When working with function pointers, it’s easier to setup a typedef so you can refer to them in an easier way.
Another tip I would give is to use std::map to store your function pointers. It provides an easy way to ‘get to’ that certain function, as you can use a human readable string for that!

I made a little example c++ program to show you:

#include <cstdlib>
#include <iostream>
#include <map>

// the function
void circleAnimation(int circleGroup, float time) { 
    printf("I'm animation 1 and was called with arguments %d, %4.2f\n", circleGroup, time);

// typedef the function pointer as 'animFuncPtr'
typedef void(*animFuncPtr)(int c, float t);

// std::map for storing function pointers
std::map<std::string, animFuncPtr> functions;

int main(int argc, char const *argv[])
    // in setup animations, store function without arguments:
    functions["anim1"] = &circleAnimation;
    // later, when calling the function, do it like this:
    functions["anim1"](1, 1.0f);

    return 0;


I needed to store member function pointers in a base class’ map and i found an article, which was very helpful. In case someone needs member function pointers, here it is.