boost::python::object 的 C++ 赋值不起作用。为什么?
C++ assignment to boost::python::object doesn't work. Why?
根据 documentation,这里的赋值应该有效,但它没有:
#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;
}
编译器输出如下:
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
文档指出:
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++.
但是,我无法让它工作。为什么我的 boost::python
不能自动转换双倍数?实际上,不只是双打,而是任何类型。
这是我的环境信息:
- OSX Yosemite
- LLVM 6.1.0
- Python 3.5.0
- 提升 1.59.0
- 日食IDE
编辑:有效
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;
}
因此看起来您不能像 ForEveR 所建议的那样在不首先创建 boost::python
对象的情况下分配 C++ 类型。
因为object
构造函数是explicit
template <class T>
explicit object(T const& x);
禁止使用像object = T();
这样的隐式转换。
您可以使用以下内容:
object test = object(2.05);
根据 documentation,这里的赋值应该有效,但它没有:
#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;
}
编译器输出如下:
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
文档指出:
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++.
但是,我无法让它工作。为什么我的 boost::python
不能自动转换双倍数?实际上,不只是双打,而是任何类型。
这是我的环境信息:
- OSX Yosemite
- LLVM 6.1.0
- Python 3.5.0
- 提升 1.59.0
- 日食IDE
编辑:有效
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;
}
因此看起来您不能像 ForEveR 所建议的那样在不首先创建 boost::python
对象的情况下分配 C++ 类型。
因为object
构造函数是explicit
template <class T>
explicit object(T const& x);
禁止使用像object = T();
这样的隐式转换。
您可以使用以下内容:
object test = object(2.05);