首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >模版过载的模糊operator<<

模版过载的模糊operator<<
EN

Stack Overflow用户
提问于 2013-09-12 13:28:54
回答 1查看 109关注 0票数 3

下面的代码是我第一次尝试使用C++11来打印可迭代的容器。它使用函数模板默认参数特性。

代码语言:javascript
复制
#include <ostream>
#include <string>
#include <utility>

template <typename T>
void print(std::ostream &o, T const &t) { o<< t; }

void print(std::ostream &o, std::string const &s){ o<< '"'<< s<< '"'; }

template <typename K, typename V>
void print(std::ostream &o, std::pair<K, V> const &p)
{
  o<< '{'; print(o, p.first);
  o<< ": "; print(o, p.second);
  o<< '}';
}

template <typename C, typename I= typename C::const_iterator>
std::ostream &operator<< (std::ostream &o, C const &c)
{
  o<< '[';
  if(c.empty()) return o<< ']';
  I b= c.begin(), e= c.end(); -- e;
  for(; b!= e; ++ b)
  {
    print(o, *b);
    o<< ", ";
  }
  print(o, *b);
  return o<< ']';
}

它在集装箱、集装箱等上运行良好,但有一个例外:

代码语言:javascript
复制
std::cout<< std::string("wtf");

使用g++4.7/8的编译中断,表示为ambiguous operator<<

是否对此代码进行了任何修正以避免歧义?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-12 13:46:59

在字符串情况下,可以使用std::enable_if禁用重载:

代码语言:javascript
复制
template <typename C, typename I= typename C::const_iterator>
typename std::enable_if<!std::is_same<C,std::string>::value,std::ostream>::type &
  operator<< (std::ostream &o, C const &c)
{
  o<< '[';
  if(c.empty()) return o<< ']';
  I b= c.begin(), e= c.end(); -- e;
  for(; b!= e; ++ b)
  {
    print(o, *b);
    o<< ", ";
  }
  print(o, *b);
  return o<< ']';
}

或者更笼统地这样做:

代码语言:javascript
复制
template <typename T>
struct is_string : std::false_type {};

template <typename Char,typename Allocator>
struct is_string<std::basic_string<Char,Allocator> > : std::true_type {};

template <typename C, typename I= typename C::const_iterator>
typename std::enable_if<!is_string<C>::value,std::ostream>::type &
  operator<< (std::ostream &o, C const &c)
{
  o<< '[';
  if(c.empty()) return o<< ']';
  I b= c.begin(), e= c.end(); -- e;
  for(; b!= e; ++ b)
  {
    print(o, *b);
    o<< ", ";
  }
  print(o, *b);
  return o<< ']';
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18765701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档