Skip to content

Instantly share code, notes, and snippets.

@haldun
Created April 22, 2014 22:53
Show Gist options
  • Save haldun/11197135 to your computer and use it in GitHub Desktop.
Save haldun/11197135 to your computer and use it in GitHub Desktop.
template experiments
//
// main.cpp
// CppDatabase
//
// Created by Haldun Bayhantopcu on 23/04/14.
// Copyright (c) 2014 Monoid. All rights reserved.
//
#include <iostream>
#include <memory>
template <typename Fun>
class Functor {
public:
Functor(const Fun& fun) : fun_{fun} {}
template<typename... Params>
auto operator()(Params&&... params) -> decltype(Fun(), void()) {
return fun_(std::forward<Params>(params)...);
}
private:
Fun fun_;
};
template<typename Fun>
Functor<Fun> make_functor(Fun f) {
return Functor<Fun>(f);
}
void TestFunction(int x, double y) {
std::cout << x << " and " << y << '\n';
}
int main(int argc, const char * argv[]) {
auto f = make_functor(TestFunction);
f(1, 4);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment