Vector literal function pointers

Quite simple really; I want to define a literal vector of function pointers. Here's what I've got:

1
2
3
4
5
6
7
8
9
10
11
12
13
void fun1 () {}
void fun2 () {}
void fun3 () {}

typedef void (*fun_ptr)();

const std::vector<fun_ptr> fun_vec = 
{
  &fun1,
  &fun2,
  &fun3
// ERROR: initialization with '{...}' is not allowed for object of type "const std::vector<fun_ptr>"
};


So... How do I do this? This sort of seemed in the right direction (maybe I have to define my own literals??): http://en.wikipedia.org/wiki/C%2B%2B11#User-defined_literals

But I didn't really know where to go from there.

Any help is appreciated!
Last edited on
Which compiler do you use? gcc 4.6.2 works fine with this code
Last edited on
I'm using Visual Studio 2012...
Are you sure it compiles? (There was a semicolon missing at the end - I've fixed it now - so it should never have compiled the way it was).
VC++ doesn't have full C++11 support, try using a better compiler. On Windows, you can use clang with MinGW (there's even a precompiled version of clang for MinGW on the clang website)
Yeah I think I might have to switch. Not looking forward to going back to open source though... Have you ever tried using clang with Visual studio? I really don't want to stop using VS if possible..
I think I'll just use std:arrays, for some reason they seem to work, and I still get the nice size awareness:

1
2
3
4
5
6
7
8
9
10
11
12
void fun1 () {}
void fun2 () {}
void fun3 () {}

typedef void (*fun_ptr)();

const std::array<fun_ptr,3> fun_vec = 
{
  &fun1,
  &fun2,
  &fun3
};
Last edited on
Actually never mind that's a terrible idea... they all have to be the same size if I want to use them as function inputs...

I think I'll just store this stuff in files and write a script to read them in, makes more sense that way anyway as they really function as "templates" that only play a role at the beginning of code execution.
Last edited on
ausairman wrote:
Yeah I think I might have to switch. Not looking forward to going back to open source though...
Not sure what useing a different compiler has to do with going open source?
ausairman wrote:
Have you ever tried using clang with Visual studio? I really don't want to stop using VS if possible..
There is a plugin for VC++2012, but as Microsoft would have it you need the professional (paid) version of VC++2012
Not sure what useing a different compiler has to do with going open source?


Well the compiler you suggested is open source...
Last edited on
That doesn't mean you have to make the code you compile with it open source.
Oh no I didn't mean that, just that I'm yet to use an open source VS plugin or IDE that actually works more than most of the time. I started programming with Eclipse and really loved it, but then when I started using VS it made me realise how much time I was spending just getting the thing to work as intended... It's not an experience I'm keen to repeat.
Topic archived. No new replies allowed.