Skip to content

Instantly share code, notes, and snippets.

@Yangff
Created July 29, 2015 21:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yangff/260b2fa5f7e84f97e73d to your computer and use it in GitHub Desktop.
Save Yangff/260b2fa5f7e84f97e73d to your computer and use it in GitHub Desktop.
// g++ aa.cpp -o aa.exe -std=c++14
#include <cstdio>
#include <functional>
#include <tuple>
using namespace std;
template<int n, class Iso, class TRet, class... TArgs>
struct _bindArgs;
template<int n, class Tuple, class Iso, class T1, class... TArgs>
struct _bindArgs<n, Tuple, Iso, T1, TArgs...> {
Iso iso; _bindArgs<n + 1, Tuple, Iso, TArgs...> next;
_bindArgs(Iso iso):iso(iso), next(_bindArgs<n + 1, Tuple, Iso, TArgs...>(iso)){}
Tuple invoke(Tuple t = Tuple()){
get<n - 1>(t) = iso.getArg(n, T1());
return next.invoke(t);
}
};
template<int n, class Tuple, class Iso>
struct _bindArgs<n, Tuple, Iso> {
Iso iso;
_bindArgs(Iso iso):iso(iso){;}
Tuple invoke(Tuple t = Tuple()){
return t;
}
};
template<class TInfo, class TRet, class... TArgs>
struct _method {
typedef function<TRet(TArgs...)> TF;
TInfo I; TF F;
_method(TInfo I, function<TRet(TArgs...)> F):I(I),F(F){;}
TRet operator () () {
return _realcall(std::index_sequence_for<TArgs...>{});
}
template<size_t ...S>
TRet _realcall(std::index_sequence<S...>){
auto iso = I.getIsolate();
tuple<TArgs...> t = _bindArgs<1, tuple<TArgs...>, decltype(iso), TArgs...>(iso).invoke();
return F(get<S>(t) ...);
};
};
// Info
class Info{
public:
class Detail_2{
public:
int getArg(int n, int placeholder){
if (n == 1){
return 99;
}
return -1;
}
int getArg(int n, double placeholder){
if (n == 2){
return 3.14;
}
return 0;
}
} detail;
Detail_2 getIsolate(){
return detail;
}
} info;
int func(int a, double b){
printf("%d %f\n", a, b);
}
int main(){
_method<Info, int, int, double> f2(info, func);
f2();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment