I have a simple class A, providing a variadic function template. This function uses private data from within A, but the function itself is public. The class goes as follows:
class A {
public:
A() :
_bla("bla: ") {
}
template <class T>
void bar(const T& value) {
std::cout << _bla << value << std::endl;
}
template <class H, class... T>
void bar(const H& value, const T&... data) {
std::cout << _bla << value << std::endl;
bar(data...);
}
private:
const std::string _bla;
};
In a separate file, named foo.hpp, I have a function foo(), that should be able to receive and use the function a.bar() as an argument:
int main(int argc, char *argv[]) {
A a;
a.bar(1, "two", 3, 4);
foo(&a.bar);
}
I'm not very sure of where to start, but I've tried the following -- which does not work. How can I do it correctly:
template <typename... T>
inline void foo(void (bar *)(const T&...)) {
unsigned int x(0), y(0), z(0);
bar(x, y, z);
}
Bonus question: is there a way to call not only:
foo(&a.bar);
but also call foo with a.bar bound to some parameters, like:
foo(&(a.bar(p1, p2));
I can simply add p1 and p2 to foo definition itself, like in:
foo(p1, p2, &a.bar);
but it would be semantically better to my purpose if I could add these parameters before.
enable_ifmess? You can just overload it asvoid bar() {}.enable_ifto tell whether the argument pack is empty. Just have one template overload for(H,T...)and one overload with no arguments that does nothing.fooaccepts a function, buta.baris not a function. It is a template. Templates have no addresses. There are functionsa.bar<int>,a.bar<double, char>etc; which one do you want to pass tofoo? Also, how do you distinguisha.bar(p1, p2)as a function bound to some parameters anda.bar(p1, p2)as a function fully applied to all parameters?