下面的代码是我第一次尝试使用C++11来打印可迭代的容器。它使用函数模板默认参数特性。
#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<< ']';
}它在集装箱、集装箱等上运行良好,但有一个例外:
std::cout<< std::string("wtf");使用g++4.7/8的编译中断,表示为ambiguous operator<<。
是否对此代码进行了任何修正以避免歧义?
发布于 2013-09-12 13:46:59
在字符串情况下,可以使用std::enable_if禁用重载:
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<< ']';
}或者更笼统地这样做:
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<< ']';
}https://stackoverflow.com/questions/18765701
复制相似问题