Skip to content

Instantly share code, notes, and snippets.

@lichray
Created September 14, 2013 15:37
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 lichray/6562999 to your computer and use it in GitHub Desktop.
Save lichray/6562999 to your computer and use it in GitHub Desktop.
Simulate std::tie's functionality for the input iterators.
#include <tuple>
template <typename Iter, typename T1>
inline auto tie_from(Iter it, T1& t1)
-> std::tuple<T1&>
{
t1 = *it;
return std::tuple<T1&>(t1);
}
template <typename Iter, typename T1, typename... T2>
inline auto tie_from(Iter it, T1& t1, T2&... t2)
-> std::tuple<T1&, T2&...>
{
t1 = *it++;
return std::tuple_cat(std::tuple<T1&>(t1), tie_from(it, t2...));
}
#include <iostream>
#include <list>
int main()
{
std::list<int> ls = { 3, 4, 5 };
int x, y;
auto t = tie_from(begin(ls), x, y);
std::cout << x << ' ' << y << std::endl;
std::get<0>(t) = 8;
std::cout << x << ' ' << y << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment