I have the following working for the simple case of a non-multi-container in a container:
template <typename MultiContainer, typename SingleContainer>
void check (const MultiContainer& container, SingleContainer& v) {
if (std::is_same<MultiContainer, typename SingleContainer::value_type>::value)
return;
if (is_container<typename MultiContainer::value_type>::value) {
std::vector<typename MultiContainer::value_type::value_type> a; // Using vector for this first draft.
for (const auto& x : container)
std::copy (x.begin(), x.end(), std::back_inserter(a));
if (std::is_same<typename MultiContainer::value_type::value_type, typename SingleContainer::value_type>::value)
v = a;
// else check<decltype(a), SingleContainer>(a, v); // Won't compile
}
}
template <typename MultiContainer, typename SingleContainer>
SingleContainer extractFromMultiContainer (const MultiContainer& container) {
SingleContainer v;
check<MultiContainer, SingleContainer>(container, v);
return v;
}
int main() {
using Multi = std::list<std::vector<int>>;
const Multi multi = { {1,2,3}, {4,5}, {6,7,8,9} };
// using Multi = std::deque<std::list<std::vector<int>>>;
// const Multi multi = { { {1,2,3}, {4,5}, {6,7,8,9} }, { {10,11}, {12,13}, {14,15,16} } };
const auto v = extractFromMultiContainer<Multi, std::vector<int>>(multi);
for (int x : v) std::cout << x << ' '; // 1 2 3 4 5 6 7 8 9
}
But I cannot get the required recursion line in the 'check' function above working to handle the container-in-container-in-container case (must work no matter how deeply nested actually). I'm using vector as SingleContainer for this first draft (I'll try to generalize that later). Incidentally, I obtained the code of is_container from a previous thread in stackoverflow Determine if a type is an STL container at compile time, so we can just take that part for granted:
template<typename T>
struct has_const_iterator {
private:
typedef char yes;
typedef struct { char array[2]; } no;
template<typename C> static yes test(typename C::const_iterator*);
template<typename C> static no test(...);
public:
static const bool value = sizeof(test<T>(0)) == sizeof(yes);
typedef T type;
};
template <typename T>
struct has_begin_end {
template<typename C> static char (&f(typename std::enable_if<
std::is_same<decltype(static_cast<typename C::const_iterator (C::*)() const>(&C::begin)),
typename C::const_iterator(C::*)() const>::value, void>::type*))[1];
template<typename C> static char (&f(...))[2];
template<typename C> static char (&g(typename std::enable_if<
std::is_same<decltype(static_cast<typename C::const_iterator (C::*)() const>(&C::end)),
typename C::const_iterator(C::*)() const>::value, void>::type*))[1];
template<typename C> static char (&g(...))[2];
static bool const beg_value = sizeof(f<T>(0)) == 1;
static bool const end_value = sizeof(g<T>(0)) == 1;
};
template<typename T>
struct is_container : std::integral_constant<bool, has_const_iterator<T>::value && has_begin_end<T>::beg_value && has_begin_end<T>::end_value> {};