I’ve been messing with creating a wrapper using templates to allow the passing of methods as arguments in other methods without having to pepper my calling code in lambda functions based on this reply but have run into an issue.
Namely, the solution compiles when the computing
and Answering
methods are not part of the class MyClass
. However, as soon as I use methods that are part of MyClass
i get the following errors.
Intellisense Error: no sutible constrcuctor exists to convert from "void (std::function<void (int)> callback)" to "std::function<void(std::function<void(int)>)>"
Compiller Error: 'MyClass::Computing': non-standard suyntax; use '&' to create pointer to member
I’m betting I’m missing something stupidly simple here.
MyClass.h
class MyClass{
MyClass();
void A(std::function<void(int)> func);
void B(int i);
}
MyClass.cpp
MyClass::MyClass(){
//Erroring Line
std::function<void(std::function<void(int)>)> question = TheWrapper(Computing);
question(Answering);
}
void MyClass::Computing(std::function<void(int)> callback){
//Thinking deep thoughts
//Answer
callback(42);
}
void MyClass::Answering(int i){
//Shrug
}
Wrapper.cpp
template<typename F, typename U>
void TheSystem(F Func, U callback) {
//Submit request in triplicate
//Burry in a peat bog
//Dig up
//Start computation of the meaning of life, the universe, and everything
Func(callback);
}
std::function<void(std::function<void(int)>)> TheWrapper(const std::function<void(std::function<void(int)>)>& Func) {
return [Func](std::function<void(int)> callback) { TheSystem(Func, callback); };
}