c++ - 如何实现这样的模板特化?

标签 c++ templates template-specialization

现在,我使用的函数“JoinStrings”只能连接数据类型 std::string。我现在需要加入整数。所以希望重构一下。但是我失败了。我很高兴听到“你不能这样做”,因为我真的不知道以这种方式重用这些代码是否合理。

调用部分:

int main(int argc, char* argv[]) {
      vector<int> integers;
      string str = JoinStrings(integers);
      cout << str << endl;
}

我没能实现的部分:

#include <string>

template <class ConstForwardIterator>
void JoinStrings(const ConstForwardIterator& begin,
                 const ConstForwardIterator& end,
                 const std::string& delimiter,
                 std::string* output) {
  output->clear();
  for (ConstForwardIterator iter = begin; iter != end; ++iter) {
    if (iter != begin) {
      output->append(delimiter);
    }   
    output->append(*iter);
  }
}

// What data type should be declared for IntegerConstForwardIterator?
template<>
void JoinStrings(const IntegerConstForwardIterator& begin,
                 const IntegerConstForwardIterator& end,
                 const std::string& delimiter,
                 std::string* output) {
  output->clear();
  for (IntegerConstForwardIterator iter = begin; iter != end; ++iter) {
    if (iter != begin) {
      output->append(delimiter);
    }   
    output->append(std::to_string(*iter));
  }
}

template <class ConstForwardIterator>
std::string JoinStrings(const ConstForwardIterator& begin,
                        const ConstForwardIterator& end,
                        const std::string& delimiter) {
  std::string output;
  JoinStrings(begin, end, delimiter, &output);
  return output;
}

template <class Container>
std::string JoinStrings(const Container& container,
                        const std::string& delimiter = " ") {
  return JoinStrings(container.begin(), container.end(), delimiter);
}

最佳答案

可以修改代码来实现你想要的。我将此问题解释为模板特化练习。 (对于推荐的替代方案,请关注 jogojapan 的评论)

前言:我期待它能工作,但(尽管它编译)它没有因为通用版本仍然是更好的匹配:

template<template <class> class Cont >
void JoinStrings(const typename Cont<int>::const_iterator& begin,
                 const typename Cont<int>::const_iterator& end,
                 const std::string& delimiter,
                 std::string* output) {
    std::clog << "specialized called" << std::endl;
    ....
}

所以,我不得不求助于旧的enable_if(为了简洁起见,我在这里使用c++11,经过一些努力可以变成C++98)。使用您的 main 代码,将调用专用版本。

解决方案

#include<type_traits>
....
template <class ConstForwardIterator, 
    class = typename std::enable_if<not std::is_same<typename std::decay<decltype(*std::declval<ConstForwardIterator>())>::type, int>::value>::type
>
void JoinStrings(const ConstForwardIterator& begin,
                 const ConstForwardIterator& end,
                 const std::string& delimiter,
                 std::string* output) {
    std::clog << "generic called" << std::endl;
    std::clog << std::trace() << std::endl;
}

template <class ConstForwardIterator, 
    class = typename std::enable_if<std::is_same<typename std::decay<decltype(*std::declval<ConstForwardIterator>())>::type, int>::value>::type,
    class Tag = void
>
void JoinStrings(const ConstForwardIterator& begin,
                 const ConstForwardIterator& end,
                 const std::string& delimiter,
                 std::string* output) {
    std::clog << "special called" << std::endl;
    ....
}

比较经典的方案是使用“标签派发”http://www.boost.org/community/generic_programming.html#tag_dispatching ,但这需要更改更多代码,例如您的 JoinString 容器版本。

受限解决方案:如果您希望代码仅适用于 std::vector,则解决方案要简单得多:

template <>
void JoinStrings(const std::vector<int>::const_iterator& begin,
                 const std::vector<int>::const_iterator& end,
                 const std::string& delimiter,
                 std::string* output)

关于c++ - 如何实现这样的模板特化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17267313/

相关文章:

c++ - 插入 std::map 时检测到 glibc(内存错误)

c++ - 对象成员和标准类型的模板函数

c++ - 查找模板类型的模板类型 C++

C++:为什么这个 constexpr 不是编译时常量

c++ - 在定义处初始化模板结构

c++ - 递归模板解释C++

android ion, ION_IOC_IMPORT 的 ioctl 返回 <0, errno = 9

c++ - 在 C++ 中连接两个 char* 的函数

c++ - 将字符串转换为大写字母时遇到问题

c++ - 将枚举数组转换为参数包