Pus*_*dre 4 c++ boost boost-python c++11
我boost::python在这个小小的mwe中使用了这个库.
#include <deque>
#include <boost/python.hpp>
typedef std::deque<long unsigned int> DequeUInt64;
BOOST_PYTHON_MODULE_INIT(tmp) {
boost::python::class_<DequeUInt64>("DequeUInt64")
.def("push_back" ,&DequeUInt64::push_back)
.def("push_front" ,&DequeUInt64::push_front);
}
Run Code Online (Sandbox Code Playgroud)
我观察到的代码与上面编译std=c++03和gnu++03,但不与c++11或c++0x.错误是:
tmp.cpp: In function 'void init_module_tmp()':
tmp.cpp:8:47: error: no matching function for call to 'boost::python::class_<std::deque<long unsigned int> >::def(const char [10], <unresolved overloaded function type>)'
.def("push_back" ,&DequeUInt64::push_back)
^
In file included [from /opt/local/include/boost/python.hpp:18:0], [from tmp.cpp:2]:
/opt/local/include/boost/python/class.hpp:223:11:
note: candidate:
template<class Derived> boost::python::class_<T, X1, X2, X3>::self&
boost::python::class_<T, X1, X2, X3>::def(const boost::python::def_visitor<Derived>&)
[with Derived = Derived;
W = std::deque<long unsigned int>;
X1 = boost::python::detail::not_specified;
X2 = boost::python::detail::not_specified;
X3 = boost::python::detail::not_specified]
self& def(def_visitor<Derived> const& visitor)
^
note: template argument deduction/substitution failed:
tmp.cpp:8:47:
note: mismatched types 'const boost::python::def_visitor<U>' and 'const char [10]'
.def("push_back" ,&DequeUInt64::push_back)
^
In file included [from /opt/local/include/boost/python.hpp:18:0], [from tmp.cpp:2]:
/opt/local/include/boost/python/class.hpp:233:11:
note: candidate:
template<class F> boost::python::class_<T, X1, X2, X3>::self&
boost::python::class_<T, X1, X2, X3>::def(const char*, F)
[with F = F;
W = std::deque<long unsigned int>;
X1 = boost::python::detail::not_specified;
X2 = boost::python::detail::not_specified;
X3 = boost::python::detail::not_specified]
self& def(char const* name, F f)
^
note: template argument deduction/substitution failed:
tmp.cpp:8:47:
note: couldn't deduce template parameter 'F'
.def("push_back" ,&DequeUInt64::push_back)
Run Code Online (Sandbox Code Playgroud)
我的提升是boost: stable 1.60.0,我的g ++是g++-mp-5 (MacPorts gcc5 5.4.0_0) 5.4.0.我可以看到,新的标准不知何故导致类型/模板推断出现问题,但说实话,我真的不明白为什么?问题是因为Boost根本没有用c ++ 11测试过吗?上面的错误信息究竟是什么意思呢?
错误消息为您提供了线索:
未解决的重载函数类型
你传递std::deque::push_back.查看参考,您可以看到有两个重载:
void push_back( const T& value );
void push_back( T&& value ); // (since C++11)
Run Code Online (Sandbox Code Playgroud)
C++ 11增加了一个新的重载.因此,将指针作为参数传递给它将变为无效.同样地push_front.请注意,即使在C++ 11之前,也允许实现添加自己的重载[需要引证].
您可以将其强制转换为适当的类型:
.def("push_back" ,static_cast<void(DequeUInt64::*)(DequeUInt64::const_reference)>(&DequeUInt64::push_back))
.def("push_front" ,static_cast<void(DequeUInt64::*)(DequeUInt64::const_reference)>(&DequeUInt64::push_front))
Run Code Online (Sandbox Code Playgroud)
我转换它而不是根据STL的"不帮助编译器说话" 明确指定模板参数.
根据Tanner的评论,如果你先将它强制转换为函数指针,你也可以使用lambda:
.def("push_back" ,+[](DequeUInt64* d, DequeUInt64::const_reference x) { return d->push_back(x); })
.def("push_front" ,+[](DequeUInt64* d, DequeUInt64::const_reference x) { return d->push_front(x); })
Run Code Online (Sandbox Code Playgroud)
问题是 C++11 添加了一个重载push_back来支持移动语义,即void push_front( T&& value );
所以编译器不知道选择哪一个 ( <unresolved overloaded function type>)。你必须把它拼出来,像这样:
boost::python::class_<DequeUInt64>("DequeUInt64")
.def<void (DequeUInt64::*)( const T&)>("push_back",&DequeUInt64::push_back)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
208 次 |
| 最近记录: |