标准库容器的通用函数模板

时间:2013-12-18 12:18:16

标签: c++ templates c++11 stl

我正在尝试编写一个简单的通用函数来迭代容器元素。每个元素都转换为std::string(无论如何)并存储在另一个地方。基本版本很简单:

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

然后有必要为值类型为std::string的容器添加特化,并将代码转换为:

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*/
    }
}

效果很好,但现在我只能使用有序容器(vectorlist等),但我还想使用setunordered_set。任何想法如何没有“复制粘贴”实现4个容器的容器?我试着玩decltype(Container)::value_type,但没有运气。

我可能会使用大多数c ++ 11功能(编译器 - VS2012或GCC 4.8.x)

4 个答案:

答案 0 :(得分:7)

这就是为什么所有标准库的算法都适用于迭代器而不是容器。

您可以更改核心功能以处理迭代器而不是容器。这将需要部分特化,这对于函数模板是不存在的,因此我们将使用委托到类的技巧:

template <typename It>
void DoIteratorWork(It start, It end)
{
  DoIteratorWork_Impl<It, typename std::iterator_traits<It>::value_type>::call(start, end);
}

template <typename It, typename ValueType>
struct DoIteratorWork_Impl
{
  static void call(It start, It end)
  {
    for (; start != end; ++start) {
      // operate on *it
    }
  }
};

template <typename It>
struct DoIteratorWork_Impl<It, std::string>
{
  static void call(It start, It end)
  {
    for (; start != end; ++start) {
      // operate on *it
    }
  }
};

如果你真的想,你可以创建一个包装器:

template <class Container>
void DoContainerWork(const Container& c)
{
  using std::begin; using std::end; // enable ADL of begin and end
  return DoIteratorWork(begin(c), end(c));
}

答案 1 :(得分:5)

您可以使用variadic-templates,例如

template<typename T, template<typename...> class Container, typename... Args>
void ContainerWork(Container<T, Args...> c)
{
}

template<template<typename...> class Container, typename... Args>
void ContainerWork(Container<std::string, Args...> c)
{
}

当然,您可以使用简单的调度

template<typename Container>
void ContainerWork(Container c, 
typename std::enable_if<!std::is_same<typename Container::value_type,
std::string>::value>::type* = 0)
{
}

template<typename Container>
void ContainerWork(Container c,
typename std::enable_if<std::is_same<typename Container::value_type,
std::string>::value>::type* = 0)
{
}

但无论如何,如果差异仅在调用转换函数时 - 您可以简单地将其重载为T而对于stringstring的版本将简单地返回此字符串。

此外,您可以将SFINAE与C ++ 11 decltype功能

一起使用
template<typename Container>
auto ContainerWork(Container c) -> 
decltype(c.begin(),
typename std::enable_if<!std::is_same<typename Container::value_type,
std::string>::value, void>::type())
{
}

template<typename Container>
auto ContainerWork(Container c) ->
decltype(c.begin(),
typename std::enable_if<std::is_same<typename Container::value_type,
std::string>::value, void>::type())
{
}

答案 2 :(得分:2)

// implement the bodies as functors to allow for partial
// specialization for value_type:
template <class value_type>
struct DoContainerWorkImpl {
  template<typename Container>
  void operator()(const Container& c) const
  {
    for (auto&& x : c) {
      // code
    }
  }
};
template <>
struct DoContainerWorkImpl<std::string> {
  template<typename Container>
  void operator()(const Container& c) const
  {
    for (std::string const& x : c) {
      // code
    }
  }
};

template <class Container>
void DoContainerWork(const Container& c)
{
  using std::begin; // enable ADL of begin
  // extract the iterator for the container c:
  typedef typename std::decay<decltype( std::begin(c) )>::type iterator;
  // extract the value type of the container:
  typedef typename std::iterator_traits<iterator>::value_type value_type;
  DoContainerWorkImpl<value_type>()( c );
}

另一种方法涉及使算法成为一件事,并确定我们是否想要使用特征类或“本地”的其他技术在算法体内调用转换。

这是对这种方法的一种尝试:

 // convert non-strings:
 template<typename T>
 std::string as_string( T&& t )
 {
   // converting code goes here
 }
 // block dangling rvalues:
 std::string as_string( std::string&& s ) { return std::move(s); }
 // pass lvalues through:
 std::string const& as_string( std::string const& s ) { return s; }
 std::string const& as_string( std::string& s ) { return s; }

现在,我们在循环体中执行此操作:

for(auto&& elem : c) {
  std::string const& s = as_string(std::forward<decltype(elem)>(elem));
  // code using s as a string
}

如果容器是std::string的容器,则编译为noop(或者,最坏的情况是一些移动),如果不是,则转换为代码。

分配给引用的临时值的生命周期扩展保留了所有临时生成的std::string,并且迭代器的左值生命周期使“迭代器返回引用的字符串容器”的生命周期足够长。

答案 3 :(得分:1)

据我了解,您可以将std::for_each与多态仿函数一起使用:

namespace detail {

    template<typename T>
    std::string do_conversion(const T& item) {
        // convert to string
    }

    template<>
    std::string do_conversion<std::string>(const std::string& item) {
        // frame in quotes
    }
}

struct convert_to_string {
    template<typename T>
    void operator()(const T& item) {
        // delegate to free function in namespace scope, because otherwise we 
        // cannot specialize
        std::string result = detail::do_conversion(item);
    }
};

// in calling code

std::for_each(std::begin(container), std::end(container), convert_to_string{});

这对你有用吗?