Created
April 22, 2014 22:53
-
-
Save haldun/11197135 to your computer and use it in GitHub Desktop.
template experiments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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