我試圖將對象存儲在std :: set中。這些對象是來自python環境的boost :: shared_ptr <>。增加值的設置不會造成任何麻煩。但是,當我嘗試擦除一個值時,即使我傳遞了相同的參考,它也不起作用。這裏有一個例子:boost :: python和set :: erase - >奇怪的行爲
#include <set>
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/python.hpp>
using namespace std;
using namespace boost;
using namespace boost::python;
struct Bar
{
Bar() {}
};
struct Foo
{
set< shared_ptr<Bar> > v_set;
shared_ptr<Bar> v_ptr;
Foo() {}
void add(shared_ptr<Bar> v_param) {
cout << "storing " << v_param << "in v_set and v_ptr" << endl;
v_set.insert(v_param);
v_ptr = v_param;
}
void del(shared_ptr<Bar> v_param) {
cout << "deleting " << v_param << endl;
if (v_param == v_ptr) {
cout << "v_param == v_ptr" << endl;
} else {
cout << "v_param != v_ptr" << endl;
}
cout << "erasing from v_set using v_param" << endl;
if (v_set.erase(v_param) == 0) {
cout << "didn't erase anything" << endl;
} else {
cout << "erased !" << endl;
}
cout << "erasing from v_set using v_ptr" << endl;
if (v_set.erase(v_ptr) == 0) {
cout << "didn't erase anything" << endl;
} else {
cout << "erased !" << endl;
}
}
};
BOOST_PYTHON_MODULE (test)
{
class_< Foo, shared_ptr<Foo> >("Foo")
.def("add",&Foo::add)
.def("remove",&Foo::del);
class_< Bar, shared_ptr<Bar> >("Bar");
}
編譯:
%> gcc -pthread -fno-strict-aliasing -march=i686 -mtune=generic -O2 -pipe -DNDEBUG -march=i686 -mtune=generic -O2 -pipe -fPIC -I/usr/include/python2.7 -c test.cpp -o test.o
%> g++ -pthread -shared -Wl,--hash-style=gnu -Wl,--as-needed build/temp.linux-i686-2.7/test.o -L/usr/lib -lboost_python -lpython2.7 -o test.so
,現在,小python腳本:
from test import *
f = Foo()
b = Bar()
f.add(b)
f.remove(b)
下面是結果:
storing 0x8c8bc58in v_set and v_ptr
deleting 0x8c8bc58
v_param == v_ptr
erasing from v_set using v_param
didn't erase anything
erasing from v_set using v_ptr
erased !
- 我店0x8e89c58設定的內部和外部,以防萬一
- 我傳遞了相同的參照兩個電話(0x8e89c58)
- 只是爲了確保我檢查如果V == VAL
- 我試圖抹掉通過使用v - 它不起作用
- 我嘗試通過使用val擦除 - 它的工作原理!
我完全失去了那裏 - 看不到是什麼造成了這一點。任何輸入?
您在使用將「value」,「val」和「v」全部作爲同一個函數中的變量名稱使您的代碼難以遵循。 –
的確如此,對不起。我希望現在更清楚。 – girodt