0

if I have a function and I want to make a pointer to it with a specific parameter I can use auto but like this:

void bar(int n){std::cout << n;}
auto foo = std::bind(bar, 2);

but if I want to make a vector of these pointers it doesn't work

std::vector<auto> v;
2
  • 2
    You're not making a pointer to a function. You're creating a bind-expression. You cannot (easily) make a container of bind-expressions, but you can make a vector of std::function<void()>. Commented Apr 12, 2016 at 23:34
  • Do you know what auto means here? Commented Apr 12, 2016 at 23:36

1 Answer 1

2

You could write vector<decltype(foo)> v; although that probably doesn't do what you want (other bind expressions may give incompatible types).

As mentioned in comments, std::function is designed for this purpose:

std::vector< std::function<void()> > v;

v.emplace_back( foo );
v.emplace_back( std::bind(bar, 2) );
v.emplace_back( []{ std::cout << 2; } );
Sign up to request clarification or add additional context in comments.

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.