Skip to content

Instantly share code, notes, and snippets.

@johnmcfarlane
Created July 14, 2017 20:35
Show Gist options
  • Save johnmcfarlane/fdb71a5baabbb9086fe7fa254658270c to your computer and use it in GitHub Desktop.
Save johnmcfarlane/fdb71a5baabbb9086fe7fa254658270c to your computer and use it in GitHub Desktop.
Fixing `std::functional`.
#include <functional>
#include <memory>
#include <stdlib.h>
#if defined(USE_ALTERNATIVE)
template<class FunctionSignature>
class function;
template<class Result, class...Args>
class function<Result(Args...)> {
public:
template<class Functor>
function(Functor const& functor) : o(new D<Functor>(functor)) {}
Result operator()(Args&&...params) const {
return (*o)(std::forward<Args>(params)...);
}
function& operator=(std::nullptr_t) { o.reset(); return *this; }
private:
struct B {
virtual ~B() = default;
virtual Result operator()(Args&&...) const = 0;
};
template<class Functor>
struct D : B {
Functor f;
D(const Functor& input) : f(input) {}
Result operator()(Args&&...params) const override {
return f(std::forward<Args>(params)...);
}
};
std::unique_ptr<B> o;
};
#else
using std::function;
#endif
int main(int argb,char**args) {
int a = atoi(args[0]);
int b = atoi(args[1]);
int c = atoi(args[2]);
int d = atoi(args[3]);
int e = atoi(args[4]);
auto f1 = function<int(int)>([=](int n) -> int {
while (n != c * a) {
args[n] += c - b * d / e;
}
return *args[3];
});
if (b&1) {
f1 = nullptr;
}
auto x = f1(15);
return x;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment