[问题] Clang 奇怪的问题,跟compare functor有关

楼主: saladim (杀拉顶)   2014-11-03 10:42:16
开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
FreeBSD 10x
Clang++ 3.5
参数 -Wall (试过 -g, -O1 -O2, 基本上问题都存在或甚至更离谱)
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
使用 stl_tree.h里面的 red black tree
问题(Question):
最近做了一些实验 主要是想看看能不能使用stl_tree.h里面的那个rbtree.
不过遇到了一个很奇怪的问题
主要是当我使用std::less<int> 当作key的比较functor时, 最后结果用inorder
traversal 或是用rbtree的begin() and end()去循序印出的结果是错误的,
用gdb进去看发现传到 std::less<int>::operator()(const int&, const int&)
的值并非正确的值(-1077945256).......
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
节录程式码: 我是直接继承 _Rb_Tree来使用它内部的function, 方便存取内部资料
MyClass
{
public:
struct Key
{
int operator()(const MyClass& class) { return class.getHead();}
};
public:
int getHead() { return head;}
private:
.......
int head;
.......
};
template<typename K,typename V, typename KofV, typenam Comp,
typename Alloc=std::allocator<V> >
MyTree:public std::_Rb_tree<K,V,KofV,Comp,Alloc>
{
public:
std::pair<std::_Rb_tree<K,V,KofV,Comp,Alloc>::iterator,bool >
insert_unique(const value_type& value){this->_M_insert_unique(value);};
}
typedef MyTree< int, MyClass, MyClass::Key, std::less<int> > Tree;
使用插入:
Tree tree;
tree.insert(MyClass(....));
= = = = = = = = = = = = = = = = = = = = = = = = = = = = =
之后我使用inorder traversal 跟 tree.begin() to tree.end() iteration,印出的
顺序是错误的. 可是!!!!!!!! 如果我把比较函数换成另外一个:
struct MyComp
{
bool operator()(int a, int b) { return a < b; }
}
顺序就对了, 注意, 红色标示处一定要是 int, 若是换成const int& 一样出错!!!
另外 std::less<int> 跟 MyComp单独使用不管参数型态为何都是正常的......
请问有谁遇过这种状况吗? 见鬼的奇怪....谁能帮忙试一下g++版本
最近FreeBSD gcc port都编译失败.....坏一阵子了......
作者: lantw44 (#######################)   2014-11-03 15:37:00
gcc port 有坏吗?
楼主: saladim (杀拉顶)   2014-11-03 17:16:00
我最近要装都compile error...先承认我没本是从那一长串错误里面去修补让他compile过 ORZ.... gcc46~5都有error
作者: lantw44 (#######################)   2014-11-05 17:40:00
我昨天才刚成功更新 gcc5,其他版本也没遇过 error可以上传一份能编译的 code 方便测试吗?
楼主: saladim (杀拉顶)   2014-11-07 10:36:00
哈囉lantw44, 已上传一份测试code 谢谢~

Links booklink

Contact Us: admin [ a t ] ucptt.com