According to documentation, the assignment here should work, but it 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 the 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 from 'double' to 'boost::python::api::object' for 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 from 'double' to 'const boost::python::api::object' for 1st argument
class object : public object_base
^
1 error generated.
make: *** [Test.o] Error 1
The documentation states that:
object msg = "%s is bigger than %s" % make_tuple(NAME,name);
Demonstrates that you can write the C++ equivalent of "format" % x,y,z in Python, which is useful since there's no easy way to do that in std C++.
However, I can't get this to work. Why isn't my boost::python
able to auto convert the double? Actually, not just doubles, but any type.
Here's my environment info:
- OSX Yosemite
- LLVM 6.1.0
- Python 3.5.0
- Boost 1.59.0
- Eclipse IDE
Edit: this 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 is bigger than %s" % make_tuple(NAME,name);
std::cout<<std::string(extract<const char*>(msg))<<std::endl;
Py_Finalize();
return 0;
}
So it's looking like you cannot assign C++ types without first creating a boost::python
object as ForEveR is suggesting.