您的位置:首页 > 其它

很久以前发现的 vc2008 的一个bug(关于模板匹配)

2009-04-18 23:44 621 查看
使用操作符重载时,出现模板匹配错误,bug 的出现很简单,下面是代码:

#include <stdio.h>
#include <string>
struct A1
{
template<class Ch, class Tr, class Al>
void operator<<(std::basic_string<Ch, Tr, Al>& x)
{
printf("void operator<<(std::basic_string<Ch, Tr, Al>& x)/n");
}
};
struct A2
{
void operator<<(std::string& x)
{
printf("void operator<<(std::string& x)/n");
}
};
struct B1 : A1
{
using A1::operator<<;
template<class T>
void operator<<(T& x)
{
printf("void operator<<(T& x)/n");
}
};
struct B2 : A2
{
using A2::operator<<;
template<class T>
void operator<<(T& x)
{
printf("void operator<<(T& x)/n");
}
};

int main(int argc, char* argv[])
{
std::string s = "abc";

B1 b1;
// 下面两行调用了不同的函数,为什么?
// these two line call different function, why?
b1 << s;
b1.operator<<(s);
printf("/n");
B2 b2;
// OK, 完全正确
// OK, all right
b2 << s;
b2.operator<<(s);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: