Skip to content

Instantly share code, notes, and snippets.

@Eyas
Created December 7, 2014 03:47
Show Gist options
  • Save Eyas/a1ba142f32ce9f50d190 to your computer and use it in GitHub Desktop.
Save Eyas/a1ba142f32ce9f50d190 to your computer and use it in GitHub Desktop.
/**
* Unpack-able variadic tuples in C++. You can use these classes to conviniently unpack
* tuple contents as an lvalue using the `unpack` function.
*
* Example:
* tuple<int, double, int> t(1, 5.0, 3);
* int x_1; double x_2; int x_3;
* unpack(x_1, x_2, x_3) = t;
*/
namespace tuples {
template<typename... Types>
class tuple;
template<>
class tuple < > {
};
template<typename First, typename... Rest>
class tuple < First, Rest... > {
public:
tuple(const First& first, const Rest&... rest)
: _item(first)
, _rest(rest...) { }
const First _item;
const tuple<Rest...> _rest;
};
template<typename... Types>
class tuple_lvalue;
template<>
class tuple_lvalue < > {
public:
tuple_lvalue& operator=(const tuple<>& assign) {
return *this;
}
};
template < typename First, typename... Rest >
class tuple_lvalue < First, Rest... > {
public:
tuple_lvalue<First, Rest...>(First& first, Rest&... rest)
: _item(first)
, _rest(rest...) { }
tuple_lvalue& operator=(const tuple<First, Rest...>& assign) {
_item = assign._item;
_rest = assign._rest;
return *this;
}
First& _item;
tuple_lvalue<Rest...> _rest;
};
template<typename... Some>
tuple_lvalue<Some...> unpack(Some&... x) {
return tuple_lvalue<Some...>(x...);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment