I'm observing compilation error "error: could not convert '{{{&Foo::print_add}, {&X::print}}}' from '' to 'std::vector'" in the below code.
Am I doing the vector insertion wrong? BTW I don't want to use push_back method. Is it possible to insert using '='. I'm having a feeling that it's syntactical mistake but not able to find it.
When googled, I found that there was a bug in earlier versions of gcc. I'm using 4.8.1 so I'm assuming the library I'm using should include the fix.
#include <functional>
#include <iostream>
#include <vector>
struct Foo {
Foo(int num) : num_(num) {}
void print_add(int i) const { std::cout << num_+i << '\n'; }
int num_;
};
struct X {
void print() {
std::cout << "I'm in X "<<std::endl;
}
};
struct XXX {
std::function<void(const Foo&,int)> a;
std::function<void(const X&)> b;
};
int main()
{
std::vector<XXX> vec =
{
{
{&Foo::print_add},
{&X::print}
}
};
}
X::print
is a non-const
member function, and therefore a pointer to it cannot be used to initialize a std::function<void(const X&)>
.