简体   繁体   English

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

[英]Why the following code compiles with `c++03` but not with `c++11`

I am using the boost::python library in this tiny mwe.我在这个小小的 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);

} 

I observed that the code above compiles with std=c++03 and gnu++03 but not with c++11 or c++0x .我观察到上面的代码使用std=c++03gnu++03编译,但不使用c++11c++0x The error is:错误是:

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)

My boost is boost: stable 1.60.0 and my g++ is g++-mp-5 (MacPorts gcc5 5.4.0_0) 5.4.0 .我的提升是boost: stable 1.60.0而我的 g++ 是g++-mp-5 (MacPorts gcc5 5.4.0_0) 5.4.0 I can see that somehow the newer standard is causing problems with type/template inference but honestly I don't really understand why?我可以看到更新的标准以某种方式导致类型/模板推断出现问题,但老实说我真的不明白为什么? Is the problem because Boost is simply not tested with c++11?问题是因为 Boost 根本没有用 c++11 测试吗? What exactly does the error message above mean anyway?无论如何,上面的错误消息到底是什么意思?

The error message gives you a clue:错误消息为您提供了一个线索:

unresolved overloaded function type未解决的重载函数类型

You pass in std::deque::push_back .你传入std::deque::push_back Looking at a reference , you can see there are two overloads:查看参考,您可以看到有两个重载:

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

C++11 added a new overload. C++11 添加了一个新的重载。 Therefore, passing a pointer to this as an argument becomes invalid.因此,将指向 this 的指针作为参数传递变得无效。 Likewise for push_front .同样对于push_front Note that even before C++11, implementations are allowed to add their own overloads [citation needed] .请注意,即使在 C++11 之前,也允许实现添加自己的重载[需要引用]

You can cast it to the appropriate type:您可以将其转换为适当的类型:

.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))

I cast it rather than specify the template argument explicitly per STL's Don't Help the Compiler talk.我强制转换而不是根据 STL 的Don't Help the Compiler talk 明确指定模板参数。

You can also use a lambda if you force it to a function pointer first, per Tanner's comment:根据 Tanner 的评论,如果先将 lambda 强制为函数指针,也可以使用 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); })

The problem is that C++11 added an overload to push_back to support move semantics ie void push_front( T&& value );问题是 C++11 给push_back添加了一个重载来支持移动语义,即void push_front( T&& value );

So the compiler doesn't know which one to pick ( <unresolved overloaded function type> ).所以编译器不知道选择哪一个( <unresolved overloaded function type> )。 You have to spell it out, like this:你必须把它拼出来,像这样:

boost::python::class_<DequeUInt64>("DequeUInt64")
.def<void (DequeUInt64::*)( const T&)>("push_back",&DequeUInt64::push_back)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM