浏览 9013 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-05-09
为了方便显示map而自定义的两个函数,出现了一个诡异的问题,感谢jinq0123@163.com告之解决方法,但是我对问题的原因还是不太清楚.
这里的Blog好像有问题,我把<该为了《........................ cpp 代码
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2007-05-10
名称查找的问题吧 …… 可能是类模板中对引用名字做两段式查找的时候按照类模板定义点来了,不太确定这个行为是否符合标准。
转去 www.cpper.com/c/ 问这个问题吧。 |
|
返回顶楼 | |
发表时间:2007-05-15
因为otream_iterator的定义在std里,所以它首先会在std里查找operator》,只要找到一个名字一样的(尽管可能参数不一样),就不会继续到global namespace里查找了
这个确实是标准规定的 |
|
返回顶楼 | |
发表时间:2007-05-15
> using namespace std; > namespace std > { > //If don't put this function into namespace std , there will be > a > compile error . > template<class _Elem,class _Traits,class K,class V> > basic_ostream<_Elem,_Traits>& operator<<( > basic_ostream<_Elem,_Traits>& s > , > const pair<K,V>& p > ) > { > return s<<p.first<<" : "<<p.second; > } > } > template<class _Elem,class _Traits,class K,class V> > basic_ostream<_Elem,_Traits>& operator<<( > basic_ostream<_Elem,_Traits>& s > , > const map<K,V>& m > ) > { > copy(m.begin(), m.end(), ostream_iterator<pair<K,V> >(s, > "\n")); > return s; > } > int main() > { > using namespace std; > map<string,string> a; > a["key1"]="value1"; > a["key2"]="value2"; > cout<<a; > system("pause"); > return 0; > } We had a similar discussion some time ago. Please read http://tinyurl.com/yvca6w for the details (If this link does not lead to the correct entry, please read contribution 14). The short version is: Don't do that! You are not allowed to add such an overload to namespace std which brings you in the realm of undefined behaviour. The above quoted thread shows in contribution 14 that there are at least two clean solutions: 1) Change your operator template overload by a corresponding function template (e.g. "print") in your user namespace. Then replace your std::copy invocation by a std::for_each call (which uses a special print functor and *not* operator<< on std::pair) or use an explicit loop. 2) Use a better customizable iterator like boost::transform_iterator which uses the print-functor mentioned in (1). Greetings from Bremen, Daniel Krügler |
|
返回顶楼 | |