Skip to content

Instantly share code, notes, and snippets.

@pandaman64
Last active December 15, 2015 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pandaman64/5230999 to your computer and use it in GitHub Desktop.
Save pandaman64/5230999 to your computer and use it in GitHub Desktop.
#include <type_traits>
#include <tuple>
#include <cstddef>
#include <string>
#include <iostream>
template<std::size_t ...Indices>
struct index_tuple{
constexpr index_tuple(){}
constexpr static index_tuple make(){
return {};
}
};
template<std::size_t N,typename Indices>
struct concat_index_tuple;
template<std::size_t N,std::size_t ...Indices>
struct concat_index_tuple<N,index_tuple<Indices...>>{
using type = index_tuple<N,Indices...>;
};
template<std::size_t Beg,std::size_t End>
struct make_index_tuple{
using type = typename concat_index_tuple<Beg,typename make_index_tuple<Beg + 1,End>::type>::type;
};
template<std::size_t End>
struct make_index_tuple<End,End>{
using type = index_tuple<End>;
};
template<typename Tuple>
using tuple_index = typename make_index_tuple<0,std::tuple_size<Tuple>::value - 1>::type;
template<typename ...Args>
struct tuple_wrapper{
using tuple_type = std::tuple<Args...>;
tuple_type tuple;
tuple_wrapper(tuple_type t) : tuple(std::move(t)){
}
template<typename T,std::size_t ...Indices>
T make_unpack(index_tuple<Indices...>){
return { std::get<Indices>(tuple)... };
}
template<typename T>
T make(){
return make_unpack<T>(tuple_index<tuple_type>::make());
}
template<typename T>
operator T(){
return make<T>();
}
};
template<typename ...Args>
tuple_wrapper<Args...> make(std::tuple<Args...> args){
return { std::move(args) };
}
int main(){
struct {int i,j,k;std::string str;} const foo = make(1,2,3,"str");
std::cout << foo.i << foo.j << foo.k << foo.str << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment