Skip to content

Instantly share code, notes, and snippets.

@tomilov
Last active December 23, 2016 08:14
Show Gist options
  • Save tomilov/1da6b6852ea794157b2262165e842cf5 to your computer and use it in GitHub Desktop.
Save tomilov/1da6b6852ea794157b2262165e842cf5 to your computer and use it in GitHub Desktop.
#include <type_traits>
#include <utility>
template< std::size_t index, typename type >
struct ref { type & value; };
template< std::size_t index, typename type >
type && get(ref< index, type > const & r)
{
return std::forward< type >(r.value);
}
template< typename F, typename ...types, std::size_t ...indices >
decltype(auto) apply_inverse(F & f, types &... values, std::index_sequence< indices... >)
{
struct : ref< indices, types >... {} refs{{values}...};
return std::forward< F >(f)(get< (sizeof...(indices) - indices - 1) >(refs)...);
}
template< typename F, typename ...types >
decltype(auto) apply_inverse(F && f, types &&... values)
{
return apply_inverse< F, types... >(f, values..., std::index_sequence_for< types... >{});
}
#include <iostream>
int main()
{
auto const print = [] (auto const &... value) -> std::ostream & { return (std::cout << ... << value); };
apply_inverse(print, 1, 2, 3) << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment