Potrzebuję zbudować wiązania Pythona dla kodu C++. Używam boost :: python i natknąłem się na problemy próbujące ujawnić klasy zawierające funkcje używające i zwracające szablony. Oto typowy przykładZwiększenie :: python Ujawnianie funkcji C++ przy użyciu i zwracaniu szablonów
class Foo
{
public:
Foo();
template<typename T> Foo& setValue(
const string& propertyName, const T& value);
template<typename T> const T& getValue(
const string& propertyName);
};
Typowy T są łańcuch, podwójne, wektor.
Po przeczytaniu documentation próbowałem używać cienkich obwolut do każdego użytego typu. Oto owijki na łańcuch i podwójne oraz odpowiadające im deklaracje klas.
Foo & (Foo::*setValueDouble)(const std::string&,const double &) =
&Foo::setValue;
const double & (Foo::*getValueDouble)(const std::string&) =
&Foo::getValue;
Foo & (Foo::*setValueString)(const std::string&,const std::string &) =
&Foo::setValue;
const std::string & (Foo::*getValueString)(const std::string&) =
&Foo::getValue;
class_<Foo>("Foo")
.def("setValue",setValueDouble,
return_value_policy<reference_existing_object>())
.def("getValue",getValueDouble,
return_value_policy<copy_const_reference>())
.def("getValue",getValueString,
return_value_policy<copy_const_reference>())
.def("setValue",setValueString,
return_value_policy<reference_existing_object>());
Kompiluje się dobrze, ale gdy próbuję użyć powiązań Pythona, otrzymuję wyjątek C++.
>>> f = Foo()
>>> f.setValue("key",1.0)
>>> f.getValue("key")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
RuntimeError: unidentifiable C++ exception
Co ciekawe, kiedy tylko narazić Foo dla wartości podwójnym lub strun, tj
class_<Foo>("Foo")
.def("getValue",getValueString,
return_value_policy<copy_const_reference>())
.def("setValue",setValueString,
return_value_policy<reference_existing_object>());
To działa dobrze. Czy czegoś brakuje?
można zajrzeć do wątku na http://mail.python.org/pipermail/cplusplus-sig/2006-February/009990.html (który porusza podobny problem: impuls owijania :: dowolny) – lijie