plasma_effectのメモ帳的ブログのようなsomething
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
#include<type_traits>
#include<tuple>
#include<iostream>
//index_tuple
template<size_t... Num>struct index_tuple{};
template<size_t Max, size_t... Nums>struct index_count:index_count<Max - 1, Max, Nums...>{};
template<size_t... Nums>struct index_count<0, Nums...>
{
using type = index_tuple<0, Nums...>;
};
//type_num
template<class Type, class... Types>struct type_num
{
static const int value = 1 + type_num<Types...>::value;
};
template<class Type>struct type_num<Type>
{
static const int value = 1;
};
//tuple_num
template<class Ty>struct tuple_num;
template<class... Types>struct tuple_num<std::tuple<Types...>>
{
static const int value = type_num<Types...>::value;
};
//func_tuple
template<class RetType, class ArgTupleTy, class FuncTy, class ArgTy>struct func_tuple_impl;
template<class RetType, class ArgTupleTy, class FuncTy, size_t... Nums>
struct func_tuple_impl<RetType, ArgTupleTy, FuncTy, index_tuple<Nums...>>
{
static RetType run(ArgTupleTy tup, FuncTy func)
{
return func(std::get<Nums>(tup)...);
}
};
template<class RetType, class ArgTupleTy, class FuncTy>
using func_tuple = func_tuple_impl<RetType, ArgTupleTy, FuncTy,
typename index_count<tuple_num<ArgTupleTy>::value - 1>::type>;
//func_switch
template<class RetType, class ArgTupleTy, class FuncTy, class... FuncTypes>
struct func_switch_impl
{
static RetType run(size_t switch_on, ArgTupleTy tup, FuncTy func, FuncTypes... funcs)
{
return (switch_on == 0 ?
func_tuple<RetType, ArgTupleTy, FuncTy>::run(tup, func)
: func_switch_impl<RetType, ArgTupleTy, FuncTypes...>
::run(switch_on - 1, tup, funcs...));
}
};
template<class RetType, class ArgTupleTy, class FuncTy>
struct func_switch_impl<RetType, ArgTupleTy, FuncTy>
{
static RetType run(size_t, ArgTupleTy tup, FuncTy func)
{
return func_tuple<RetType, ArgTupleTy, FuncTy>::run(tup, func);
}
};
template<class RetType, class ArgTupleTy, class... FuncTypes>
inline RetType func_switch(size_t switch_on, ArgTupleTy tup, FuncTypes... funcs)
{
return func_switch_impl<RetType, ArgTupleTy, FuncTypes...>::run(switch_on, tup, funcs...);
}
//example
auto x = [](int, int, int)->int{return 0; };
auto y = [](int a, int b, int c)->int{return a + b + c; };
auto z = [](int, int, int)->int{return 1; };
int main()
{
auto w = func_switch<int>(1, std::tuple<int, int, int>(1, 2, 3), x, y, z);
std::cout << w << std::endl;
std::cin.get();
}
カレンダー
カテゴリー
フリーエリア
最新CM
最新記事
プロフィール
ブログ内検索
P R