Amusing.
What you seem to wish for is actually a kind of iterator, that would iterate over the given ranges, and at each step gives you a permutation.
It can, typically, be written without metaprogramming, especially since variadic templates are only supported since C++0x. Nonetheless it's a very interesting challenge I feel.
Our first helper here is going to be little tuple class. We are also going to need a number of meta-template programming trick to transform one tuple into another, but I'll let it as an exercise for the reader to write both the meta-template functions necessary and the actual functions to execute the transformation (read: it's much too hot this afternoon for me to get to it).
Here is something to get you going.
template <class... Containers>
class permutation_iterator
{
public:
// tuple of values
typedef typename extract_value<Containers...>::type value_type;
// tuple of references, might be const references
typedef typename extract_reference<Containers...>::type reference;
// tuple of pointers, might be const pointers
typedef typename extract_pointer<Containers...>::type pointer;
permutation_iterator(Containers&... containers) { /*extract begin and end*/ }
permutation_iterator& operator++()
{
this->increment<sizeof...(Containers)-1>();
return *this;
}
private:
typedef typename extract_iterator<Containers...>::type iterator_tuple;
template <size_t N>
typename std::enable_if_c<N < sizeof...(Containers) && N > 0>::type
increment()
{
assert(mCurrent.at<N>() != mEnd.at<N>());
++mCurrent.at<N>();
if (mCurrent.at<N>() == mEnd.at<N>())
{
mCurrent.at<N>() = mBegin.at<N>();
this->increment<N-1>();
}
}
template <size_t N>
typename std::enable_if_c<N == 0>::type increment()
{
assert(mCurrent.at<0>() != mEnd.at<0>());
++mCurrent.at<0>();
}
iterator_tuple mBegin;
iterator_tuple mEnd;
iterator_tuple mCurrent;
};
If you don't know how to go meta, the easier way is to go recursive, then require the user to indicate which container she wishes to access via a at method taking a N as parameter to indicate the container index.