Skip to content

Instantly share code, notes, and snippets.

@benloong
Created March 18, 2014 09:26
Show Gist options
  • Save benloong/9616546 to your computer and use it in GitHub Desktop.
Save benloong/9616546 to your computer and use it in GitHub Desktop.
c++11 meta sequence
template<typename T, T... _Rest>
struct array_pack;
template<typename T>
struct array_pack<T>{};
template<typename T, T _This, T... _Rest>
struct array_pack<T, _This, _Rest...> : private array_pack<T, _Rest...>
{
const T & operator[](const int pos)
{
//as cpp memory layout, indexing reversely
return ((T *)this)[sizeof...(_Rest)-pos];
}
constexpr int size() const
{
return sizeof...(_Rest)+1;
}
private:
T _this = _This;
};
@benloong
Copy link
Author

//use like this
array_pack<int, 1, 10, 100, 1000> pack;
int sz = pack.size();
int a0 = pack[0];
int a1 = pack[1];
int a2 = pack[2];
int a3 = pack[3];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment