Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
std::mem_fn
#include <functional>
#include <iostream>
class State {
public:
int i = 5;
};
class Foo {
public:
void display_greeting() {
std::cout << "Hello, world.\n";
}
void display_number(int i) {
std::cout << "number: " << i << '\n';
}
int data = 7;
double calc(const State& s) {
return s.i + 10;
}
double getOutput(const State& s) {
return _func(s);
}
/*double getOutM(const State& s) {
return _funcM(this, s);
}*/
void constructOutput(std::function<double(const State&)> func) {
_func = func;
}
/*void constructOutM(std::function<double(const State&)> funcM) {
_funcM = funcM;
}*/
private:
std::function<double(const State&)> _func;
//std::mem_fn<double (Foo::*)(const State&)> _funcM;
};
class Bar {
public:
};
int main() {
Foo f;
auto greet = std::mem_fn(&Foo::display_greeting);
greet(f);
auto print_num = std::mem_fn(&Foo::display_number);
print_num(f, 42);
print_num + 1;
//std::cout << decltype(print_num).name() << std::endl;
auto access_data = std::mem_fn(&Foo::data);
std::cout << "data: " << access_data(f) << '\n';
f.constructOutput(std::bind(&Foo::calc, &f, std::placeholders::_1));
State s1;
std::cout << f.getOutput(s1) << std::endl;
}
#include <functional>
#include <iostream>
class State {
public:
double i = 0.1;
};
class Foo {
public:
int data = 1;
double calc(const State& s) const {
return data + s.i;
}
double getOutput(const State& s) {
return _func(s);
}
template<typename T, typename Class>
void constructOutput(T(Class::*const memfunc)(const State& s) const) {
_func = std::bind(memfunc, static_cast<Class*>(this),
std::placeholders::_1);
}
// _func = [this](const State&s) { memfunc(this, s) }
private:
std::function<double(const State&)> _func;
};
int main() {
Foo o;
State s;
o.constructOutput(&Foo::calc);
std::cout << o.getOutput(s) << std::endl;
auto oCopy(o);
oCopy.data = 10;
std::cout << oCopy.getOutput(s) << std::endl;
}
#include <functional>
#include <iostream>
class State {
public:
double i = 0.1;
};
class Foo {
public:
int data = 1;
Foo() {}
Foo(const Foo&) {}
double calc(const State& s) const {
return data + s.i;
}
double getOutput(const State& s) {
return _func(s);
}
template<typename T, typename Class>
int constructOutput(T(Class::*const memfunc)(const State& s) const) {
_func = std::bind(memfunc, static_cast<Class*>(this),
std::placeholders::_1);
return 0;
}
// _func = [this](const State&s) { memfunc(this, s) }
private:
std::function<double(const State&)> _func;
int hack {
constructOutput(&Foo::calc)
};
};
int main() {
Foo o;
State s;
//o.constructOutput(&Foo::calc);
std::cout << o.getOutput(s) << std::endl;
auto oCopy(o);
oCopy.data = 10;
std::cout << oCopy.getOutput(s) << std::endl;
}
#include <functional>
#include <iostream>
class State {
public:
double i = 0.1;
};
class Output {
public:
Output() {}
Output(std::function<double(const State&)> func) : _func(func) {}
double eval(const State& s) {
return _func(s);
}
std::function<double(const State&)> _func;
};
class Foo {
public:
int data = 1;
double calc(const State& s) const {
return data + s.i;
}
Output* getOutput() {
return &_outp;
}
template<typename T, typename Class>
void constructOutput(T(Class::*const memfunc)(const State& s) const) {
_outp = Output(std::bind(memfunc, static_cast<Class*>(this),
std::placeholders::_1));
}
private:
Output _outp;
};
class Reporter {
public:
void setInput(Output* out) {
_out = out;
}
double eval(const State& s) {
return _out->eval(s);
}
private:
Output* _out;
};
int main() {
Foo o;
State s;
o.constructOutput(&Foo::calc);
Reporter r;
r.setInput(o.getOutput());
std::cout << r.eval(s) << std::endl;
auto oCopy(o);
r.setInput(oCopy.getOutput());
oCopy.data = 10;
std::cout << r.eval(s) << std::endl;
}
#include <functional>
#include <iostream>
class State {
public:
double i = 0.1;
};
class Foo;
class AbstractOutput {
public:
virtual double eval(const State& s) = 0;
virtual void setParent(Foo* foo) = 0;
};
template <typename FooType>
class Output : public AbstractOutput {
public:
Output(FooType* obj,
double(FooType::*const memfunc)(const State& s) const) :
_obj(obj), _func(memfunc) { }
double eval(const State& s) override {
return _func(_obj, s);
}
void setParent(Foo* foo) override {
auto x = static_cast<FooType*>(foo);
_obj = x;
}
private:
std::function<double(FooType*, const State& s)> _func;
FooType* _obj;
};
class Foo {
public:
Foo() {}
Foo(const Foo& src) : data(src.data), _output(src._output) {
_output->setParent(this);
}
int data = 1;
double calc(const State& s) const {
return data + s.i;
}
double getOutput(const State& s) {
return _output->eval(s);
}
template<typename Class>
void constructOutput(double(Class::*const memfunc)(const State& s) const) {
_output = new Output<Class>(dynamic_cast<Class*>(this), memfunc);
// _func = memfunc;
}
// _func = [this](const State&s) { memfunc(this, s) }
virtual void VIRTUALLLLL() {}
private:
AbstractOutput* _output;
};
class Bar : public Foo {
public:
int numm = 1;
double calcBar(const State& s) const {
return numm + 1010;
}
};
int main() {
Bar o;
State s;
o.constructOutput(&Bar::calcBar);
std::cout << o.getOutput(s) << std::endl;
auto oCopy(o);
oCopy.numm = 10;
std::cout << oCopy.getOutput(s) << std::endl;
}
#include <functional>
#include <iostream>
class State {
public:
double i = 0.1;
};
class Foo {
public:
int data = 1;
double calc(const State& s) const {
return data + s.i;
}
double getOutput(const State& s) {
return _func(this, s);
}
/*double getOutM(const State& s) {
return _funcM(this, s);
}*/
template<typename T, typename Class>
void constructOutput(T(Class::*const memfunc)(const State& s) const) {
_func = memfunc;
}
// _func = [this](const State&s) { memfunc(this, s) }
private:
std::function<double(Foo*, const State&)> _func;
};
int main() {
Foo o;
State s;
o.constructOutput(&Foo::calc);
std::cout << o.getOutput(s) << std::endl;
auto oCopy(o);
oCopy.data = 10;
std::cout << oCopy.getOutput(s) << std::endl;
}
#include <functional>
#include <iostream>
class Foo {
public:
void mytype() {
std::cout << typeid(decltype(this)).name() << std::endl;
}
virtual void hi() {
}
};
class Bar : public Foo {
};
int main() {
Foo f;
f.mytype();
Bar b;
b.mytype();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment