Question: I have a type that can be define as a vector of vector of vector ... of vector of an integral type. Example:

```
std::vector<std::vector<std::vector<std::vector< std::vector<signed char> > > > > _data;

```

I'm searching for an elegant way to determine the number of non-empty vector at the deeper level. I could do that for that example using a 4 encapsulate loop like

```
for (it0 = data.cbegin() ; it0 != _data.cend() ; ++it0)
 for (it1 = *it0.cbegin() ; it1 != *it0.cend() ; ++it1)
   for (it2 = *it1.cbegin() ; it2 != *it1.cend() ; ++it2)
      for (it3 = *it2.cbegin() ; it3 != *it2.cend() ; ++it3)
          nonEmpty += (unsigned int) (*it3.empty());

```

But how can I create a template (to support vector, list or any kind of container sharing the same API) function to do that for any deep (more than 4 level) ? I think a recursion is the correct way, but don't know how to do that with Template ...

All help & advice will be welcome because I quite sure that there is more than one solution to that.

Answer: 
Maybe Something like that ? (it uses `std::enable_if` so it's a C++11 answer, but maybe you can use the boost one instead).

```
template <typename T, typename U>
typename std::enable_if<std::is_same<typename U::value_type,T>::value,std::size_t>::type
non_empties(const U& r)
{
    return !r.empty();
}

template <typename T, typename U>
typename std::enable_if<!std::is_same<typename U::value_type,T>::value,std::size_t>::type
non_empties(const U& r)
{
    std::size_t res = 0;
    for(auto& it : r)
    {
        res += non_empties<T>(it);
    }
    return res;
}

```

usage : 

```
auto non_empty_terminal = non_empties<signed char>(_data);

```

You have to put the 'stop' type as a template parameter, so maybe it's not ideal.

Live exemple [here](http://ideone.com/nKhPyF)
You can do pattern matching to count depthness

```
template <typename Base>
struct NestedVectors //non vector match
{
   static const size_t depth=0;
   static size_t count_empty(const Base& b)
   {
      throw std::string("cant be here");
    }

};

template <class Rv>
struct NestedVectors<std::vector<Rv> >
{
   static const size_t depth=1 + NestedVectors<Rv>::depth ;
   static size_t count_empty(const std::vector<Rv>& vec)
   {
       size_t r=0;
       if(NestedVectors<Rv>::depth == 0)
       {
            if(vec.empty())
                return 1;
       }
       else
       {
           for(size_t i =0; i < vec.size() ; ++i)
           {
               r+=NestedVectors<Rv>::count_empty(vec[i]);
           }
       }
       return r;
   }
};
int main()
{
    typedef std::vector<
       std::vector<
         std::vector<
           std::vector<
             std::vector<
               signed char
        > > > > > data_t;
   data_t t;
   std::cout << NestedVectors<data_t>::depth << " " << NestedVectors<data_t>::count_empty(t);
}

```