为什么以下代码使用`c ++ 03`而不是`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);

} 

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

我的助推器是,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)

C ++ 11添加了新的重载。因此,将指向它的指针作为参数传递将变得无效。同样适用于push_front请注意,即使在C ++ 11之前,也允许实现添加其自己的重载[citation needed]

您可以将其转换为适当的类型:

.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的“不帮助编译器”一文明确指定模板参数

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么以下代码使用`c ++ 03`而不是`c ++ 11`进行编译

来自分类Dev

为什么以下代码使用clang而不是gcc进行编译

来自分类Dev

为什么在以下代码中出现“必须是可修改的L值”编译器错误(C2105)?

来自分类Dev

为什么以下代码无法编译?

来自分类Dev

为什么以下代码在C ++中是非法的

来自分类Dev

以下代码在C中做什么?

来自分类Dev

为什么使用#而不是注释而不是注释。以下代码中的元素?

来自分类Dev

为什么clang ++不编译以下代码?

来自分类Dev

为什么以下代码编译和执行成功?

来自分类Dev

为什么clang ++不编译以下代码?

来自分类Dev

为什么以下代码可以编译并成功运行?

来自分类Dev

为什么以下代码打印出10而不是null?

来自分类Dev

为什么以下代码打印5而不是6?

来自分类Dev

在以下代码的上下文中,为什么“ cout << c”是合法的而“ c =“ x””是非法的?

来自分类Dev

为什么以下代码在C,Python中给出不同的输出?

来自分类Dev

是否可以将使用c ++ 11编译的库链接到需要使用c ++ 03编译的代码

来自分类Dev

学习C ++指针会与以下代码一起进入核心转储,我真的不为什么吗?

来自分类Dev

在C ++中执行以下代码以进行函数重载时,不会输出输出

来自分类Dev

为什么此代码段适用于C ++ 17,而编译器在使用C ++ 11时却抱怨呢?

来自分类Dev

在 C++11 中,是否可以在编译时使用运行时甚至编译时限制条件进行 for 循环?

来自分类Dev

Java类型清除:为什么以下代码段无法编译?

来自分类Dev

为什么以下代码在iPhone 5而不是iPhone 5S上崩溃?

来自分类Dev

为什么以下代码不会导致对象移动而不是复制?

来自分类Dev

为什么该程序使用gcc而不是g ++进行编译?

来自分类Dev

简单的C ++代码...为什么要编译?

来自分类Dev

在C ++中使用<cstdio>而不是<stdio.h>时,为什么“ std :: printf”和“ printf”都进行编译?

来自分类Dev

为什么“ bool c = nullptr”;编译(C ++ 11)?

来自分类Dev

c ++中的预处理程序指令:以下代码的输出是什么?

来自分类Dev

试图了解C中的以下代码有什么问题

Related 相关文章

  1. 1

    为什么以下代码使用`c ++ 03`而不是`c ++ 11`进行编译

  2. 2

    为什么以下代码使用clang而不是gcc进行编译

  3. 3

    为什么在以下代码中出现“必须是可修改的L值”编译器错误(C2105)?

  4. 4

    为什么以下代码无法编译?

  5. 5

    为什么以下代码在C ++中是非法的

  6. 6

    以下代码在C中做什么?

  7. 7

    为什么使用#而不是注释而不是注释。以下代码中的元素?

  8. 8

    为什么clang ++不编译以下代码?

  9. 9

    为什么以下代码编译和执行成功?

  10. 10

    为什么clang ++不编译以下代码?

  11. 11

    为什么以下代码可以编译并成功运行?

  12. 12

    为什么以下代码打印出10而不是null?

  13. 13

    为什么以下代码打印5而不是6?

  14. 14

    在以下代码的上下文中,为什么“ cout << c”是合法的而“ c =“ x””是非法的?

  15. 15

    为什么以下代码在C,Python中给出不同的输出?

  16. 16

    是否可以将使用c ++ 11编译的库链接到需要使用c ++ 03编译的代码

  17. 17

    学习C ++指针会与以下代码一起进入核心转储,我真的不为什么吗?

  18. 18

    在C ++中执行以下代码以进行函数重载时,不会输出输出

  19. 19

    为什么此代码段适用于C ++ 17,而编译器在使用C ++ 11时却抱怨呢?

  20. 20

    在 C++11 中,是否可以在编译时使用运行时甚至编译时限制条件进行 for 循环?

  21. 21

    Java类型清除:为什么以下代码段无法编译?

  22. 22

    为什么以下代码在iPhone 5而不是iPhone 5S上崩溃?

  23. 23

    为什么以下代码不会导致对象移动而不是复制?

  24. 24

    为什么该程序使用gcc而不是g ++进行编译?

  25. 25

    简单的C ++代码...为什么要编译?

  26. 26

    在C ++中使用<cstdio>而不是<stdio.h>时,为什么“ std :: printf”和“ printf”都进行编译?

  27. 27

    为什么“ bool c = nullptr”;编译(C ++ 11)?

  28. 28

    c ++中的预处理程序指令:以下代码的输出是什么?

  29. 29

    试图了解C中的以下代码有什么问题

热门标签

归档