gpt4 book ai didi

c++ - 为什么下面的代码用 `c++03` 编译而不用 `c++11`

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:06:32 24 4
gpt4 key购买 nike

我在这个微型 mwe 中使用 boost::python 库。

#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);

}

我观察到上面的代码可以用 std=c++03gnu++03 编译,但不能用 c++11c++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)

我的 boost 是 boost: stable 1.60.0,我的 g++ 是 g++-mp-5 (MacPorts gcc5 5.4.0_0) 5.4.0。我可以看到更新的标准以某种方式导致类型/模板推断出现问题,但老实说我真的不明白为什么?问题是因为 Boost 根本没有用 c++11 进行测试吗?上面的错误消息到底是什么意思?

最佳答案

错误信息给了你一个线索:

unresolved overloaded function type

您传入 std::deque::push_back。看着 reference ,你可以看到有两个重载:

void push_back( const T& value );
void push_back( T&& value ); // (since C++11)

C++11 添加了一个新的重载。因此,将指向 this 的指针作为参数传递变得无效。 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))

我转换它而不是根据 STL 的 Don't Help the Compiler 显式指定模板参数说话。

根据 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); })

关于c++ - 为什么下面的代码用 `c++03` 编译而不用 `c++11`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38536760/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com