[問題] 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