C++ assignment to boost::python::object doesn't work. Why? -
according documentation, assignment here should work, doesn't:
#include <boost/python.hpp> #include <iostream> int main(int, char **) { using namespace boost::python; py_initialize(); object test = object(2.05); //this works fine test = 3.05; //compiler error std::cout << extract<double>(test) << std::endl; py_finalize(); return 0; }
here's compiler output:
g++ -std=c++1y -i/usr/local/cellar/python3/3.5.0/frameworks/python.framework/versions/3.5/include/python3.5m -i/usr/local/cellar/boost/1.59.0/include -o0 -g3 -wall -c -fmessage-length=0 -mmd -mp -mf"test.d" -mt"test.d" -o "test.o" "../test.cpp" ../test.cpp:9:10: error: no viable overloaded '=' test = 3.05; ~~~~ ^ ~~~~ /usr/local/cellar/boost/1.59.0/include/boost/python/object_core.hpp:241:9: note: candidate function (the implicit move assignment operator) not viable: no known conversion 'double' 'boost::python::api::object' 1st argument class object : public object_base ^ /usr/local/cellar/boost/1.59.0/include/boost/python/object_core.hpp:241:9: note: candidate function (the implicit copy assignment operator) not viable: no known conversion 'double' 'const boost::python::api::object' 1st argument class object : public object_base ^ 1 error generated. make: *** [test.o] error 1
the documentation states that:
object msg = "%s bigger %s" % make_tuple(name,name);
demonstrates can write c++ equivalent of "format" % x,y,z in python, useful since there's no easy way in std c++.
however, can't work. why isn't boost::python
able auto convert double? actually, not doubles, type.
here's environment info:
- osx yosemite
- llvm 6.1.0
- python 3.5.0
- boost 1.59.0
- eclipse ide
edit: works
int main(int, char **) { py_initialize(); //object test = object(2.05); //this works fine //test = 3.05; //compiler error str name = "test"; str name = name.upper(); object msg = "%s bigger %s" % make_tuple(name,name); std::cout<<std::string(extract<const char*>(msg))<<std::endl; py_finalize(); return 0; }
so it's looking cannot assign c++ types without first creating boost::python
object forever suggesting.
because object
constructor explicit
template <class t> explicit object(t const& x);
it's forbidden use implicit conversion object = t();
. can use following thing:
object test = object(2.05);
Comments
Post a Comment