[C++-sig] How to override a property setter?
I got a class: class X { private: std::wstring text_; public: std::wstring text() const { return text_; } void set_text(const std::string& text) { text_ = from_utf8(text); } void set_text(const std::wstring& text) { text_ = text; } }; I'm trying to use it as a property: BOOST_PYTHON_MODULE(test) { class_("X") .add_property("text", &X::text, ) ; } What should I write to get following behavior: >>> x = X() >>> x.text = "abc" # I want X::set_text(const std::string&) to be invoked here >>> x.text = u"abc" # Here I expect X::set_text(const std::wstring&) to be called -- Ilya. ___ Cplusplus-sig mailing list Cplusplus-sig@python.org https://mail.python.org/mailman/listinfo/cplusplus-sig
[C++-sig] Weird function call results
I got an overloaded function: void f(int n) { std::clog << "invoked f(int n = " << n << ")" << std::endl; } void f(double d) { std::clog << "invoked f(double d = " << d << ")" << std::endl; } If I put declarations in that order: void (*f_int)(int) = &f; void (*f_double)(double) = &f; BOOST_PYTHON_MODULE(test) { def("f", f_int); def("f", f_double); } No matter what I pass into the function f, f(double) is invoked every time. Even though I call f(int(1)) I will see "invoked f(double d = 1)". But if I declare the function f in reversed order: BOOST_PYTHON_MODULE(test) { def("f", f_double); def("f", f_int); } I will see "invoked f(int n = 5)" when I call f(5) and "invoked f(double d = 3.14)" when I call f(3.14) as it has to be. Why does it happen? Why does it depend on declaration order? -- Ilya. ___ Cplusplus-sig mailing list Cplusplus-sig@python.org https://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] Weird function call results
Alex, > It's not a bug, it's as designed. Boost.python tries function overloads > in reverse registration order and picks the first one that works, in the > sense that all the arguments convert. Is that behavior specified somewhere in the documentation? ___ Cplusplus-sig mailing list Cplusplus-sig@python.org https://mail.python.org/mailman/listinfo/cplusplus-sig