Generalised function template for standard library containers

Viewed 353

I'm trying to write a simple generalised function for iteration over container elements. Every element is converted to std::string (no matter how) and stored in another place. The basic version was trivial:

template<class Container>
void ContainerWork(const Container& c)
{
    for(const auto& elem : c) {
        /* convert to string and store*/
    }
}

Then it became necessary to add specialisation for containers with value type std::string and code transformed to:

template<typename T, template<typename, typename> class Container, class Allocator>
void ContainerWork(Container<T, Allocator> c)
{
    for(const T& elem : c) {
        /* convert to string and store*/
    }
}

template<template<typename, typename> class Container, class Allocator>
void ContainerWork(Container<std::string, Allocator> c)
{
    for(const std::string& elem : c) {
        /* frame elem in quotes*/
    }
}

It works great, but now I can use only sequenced containers (vector, list, etc.), but I want also use set and and unordered_set. Any ideas how do this without "copy-paste" realisation for container with 4 params? I am trying to play with decltype(Container)::value_type but with no luck.

I may use most of c++11 features (compiler - VS2012 or GCC 4.8.x)

4 Answers
Related