我有,我已经超负荷了我的调试类两种功能:重载函数没有被使用:
template<class IteratorT>
inline debug&
operator()(const std::string& name,
IteratorT begin,
IteratorT end)
{
_stream << indent(internal) << "g< " << name << " : [ ";
for (auto i = begin; i != end; i++)
_stream << (*i) << " ";
_stream << "] >" << std::endl;
return *this;
}
而且
inline debug&
operator()(const std::string& name,
std::vector<uint8_t>::const_iterator begin,
std::vector<uint8_t>::const_iterator end)
{
_stream << indent(internal) << "u8< " << name << " : [ " << std::hex;
std::copy(begin, end, std::ostream_iterator<uint32_t>(_stream, " "));
_stream << "] >" << std::endl;
return *this;
}
下面是一个剪断的如何使用它:
int main()
{
debug log;
std::vector<uint8_t> vec;
vec.push_back(0xde);
vec.push_back(0xad);
vec.push_back(0xc0);
vec.push_back(0xde);
log("vec", vec.begin(), vec.end());
}
输出是(因为它没有被打印为十六进制字符,我忽略了未格式化的结果):
g< "vec" : [ ... ] >
而不是
u8< "vec" : [ de ad c0 de ] >
出于某种原因编译器不采摘正确的,重载函数。
$ g++47 --version
g++47 (GCC) 4.7.0 20120224 (experimental)
好的。在旁注中,超载是否比部分专业化更好? – nerozehl 2012-02-27 17:32:34
如果您希望所有向量迭代器在一般情况下具有特殊行为,将使用部分专用化。不知道是否有可能部分专注于value_type为uint8_t – CashCow 2012-02-27 17:43:43
@nerozehl的迭代器:你不能部分地专门化一个函数,所以这个问题有一个明确的简单答案......可能与不可能。现在,您可以将该函数移动为模板类的静态非模板成员函数,在这种情况下,您可以部分专门化,以额外代码为代价获得灵活性。但我不确定你会从中得到什么。另一方面,如果你的意思是一个*完整的*专业化的功能,过载应该是首选,但我不记得为什么/它在哪里重要的具体细节...谷歌它:) – 2012-02-27 17:44:57