Vector literal function pointers

Mar 1, 2013 at 2:51pm
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 Mar 2, 2013 at 1:06am
Mar 1, 2013 at 3:15pm
Which compiler do you use? gcc 4.6.2 works fine with this code
Last edited on Mar 1, 2013 at 3:16pm
Mar 2, 2013 at 1:06am
I'm using Visual Studio 2012...
Mar 2, 2013 at 1:07am
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).
Mar 2, 2013 at 1:18am
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)
Mar 2, 2013 at 5:08am
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..
Mar 2, 2013 at 5:23am
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 Mar 2, 2013 at 5:23am
Mar 2, 2013 at 5:29am
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 Mar 2, 2013 at 5:34am
Mar 3, 2013 at 2:43am
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
Mar 7, 2013 at 11:43pm
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 Mar 7, 2013 at 11:43pm
Mar 8, 2013 at 5:16am
That doesn't mean you have to make the code you compile with it open source.
Mar 8, 2013 at 7:03am
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.