C++ 调用另一个函数重载
我想了解C++ 调用另一个函数重载,c++,templates,boost,overloading,odeint,C++,Templates,Boost,Overloading,Odeint,我想了解c++boost库中的odeint,我需要知道哪个部分做什么。 在boost/numeric/odeint/integrate/integrate\u adaptive.hpp中,有一个名为integrate\u adaptive的函数。此函数有一些重载。通过我的一些操作,简化的文件如下所示: 集成\u自适应\u最小.hpp #define BOOST_NUMERIC_ODEINT_INTEGRATE_INTEGRATE_ADAPTIVE_HPP_INCLUDED #include &
c++boost
库中的odeint
,我需要知道哪个部分做什么。
在boost/numeric/odeint/integrate/integrate\u adaptive.hpp
中,有一个名为integrate\u adaptive
的函数。此函数有一些重载。通过我的一些操作,简化的文件如下所示:
集成\u自适应\u最小.hpp
#define BOOST_NUMERIC_ODEINT_INTEGRATE_INTEGRATE_ADAPTIVE_HPP_INCLUDED
#include <boost/type_traits/is_same.hpp>
#include <boost/numeric/odeint/stepper/stepper_categories.hpp>
#include <boost/numeric/odeint/integrate/null_observer.hpp>
#include <boost/numeric/odeint/integrate/detail/integrate_adaptive.hpp>
using namespace std;
namespace boost {
namespace numeric {
namespace odeint {
/*
* the two overloads are needed in order to solve the forwarding problem
*/
template< class Stepper , class System , class State , class Time , class Observer >
size_t integrate_adaptive(
Stepper stepper , System system , State &start_state ,
Time start_time , Time end_time , Time dt ,
Observer observer )
{
cout<<"type one"<<endl; //added by me ***************************************
typedef typename odeint::unwrap_reference< Stepper >::type::stepper_category stepper_category;
return detail::integrate_adaptive(
stepper , system , start_state ,
start_time , end_time , dt ,
observer , stepper_category() );
/*
* Suggestion for a new extendable version:
*
* integrator_adaptive< Stepper , System, State , Time , Observer , typename Stepper::stepper_category > integrator;
* return integrator.run( stepper , system , start_state , start_time , end_time , dt , observer );
*/
}
/**
* \brief Second version to solve the forwarding problem,
* can be called with Boost.Range as start_state.
*/
template< class Stepper , class System , class State , class Time , class Observer >
size_t integrate_adaptive(
Stepper stepper , System system , const State &start_state ,
Time start_time , Time end_time , Time dt ,
Observer observer )
{
cout<<"type two"<<endl; //added by me ***************************************
typedef typename odeint::unwrap_reference< Stepper >::type::stepper_category stepper_category;
return detail::integrate_adaptive(
stepper , system , start_state ,
start_time , end_time , dt ,
observer , stepper_category() );
}
} // namespace odeint
} // namespace numeric
} // namespace boost
编译和运行时:
g++ -std=c++11 minimal.cpp -larmadillo -lboost_thread -lboost_filesystem -lboost_system -Wfatal-errors
./a.out
结果表明,调用了第一个函数integrate\u adaptive
,该函数没有const
。现在,我想知道调用第二个函数的正确方法是什么?对象x不能是常量
如果有人知道boost
library的机制,那么告诉我第二个函数的优点是值得赞赏的
更新:
原始代码:,您只需将
x
转换为const
即可调用integrate\u adaptive()
integrate_adaptive(使_受控(1E-10,1E-10,步进机类型()),
系统,常数(x),0.0,11.0,0.1,观察者);
函数的const版本承诺在其作用域内不会修改x的内容,但是main()
可以在integrate\u adaptive()
返回后继续修改它
(使用C++风格的
const_cast
的优点是,如果您决定将x的类型更改为其他类,那么您可以更快地发现自己。)老实说,我对这个问题的前提有点困惑
integrade\u adaptive
最终调用odeint::copy
(来自odeint/util/copy.hpp)来自controlled\u step\u result try\u step(System,const StateInOut&x,time\u type&,time\u type&)
。此处的文件说明(除其他外)
系统需要解决的系统功能,因此是ODE的r.h.s。它必须满足\param
的概念简单系统
x应解决的ODE状态如果 该步骤成功。可以是一个助推范围\param
const
。这看起来很奇怪,但是如果你愿意的话,用double*const&
(与double-const*const&
)可以想象得到
更新
文档中说,对于常量重载:
/*
* the two overloads are needed in order to solve the forwarding problem
*/
还有,它说
/**
* \brief Second version to solve the forwarding problem,
* can be called with Boost.Range as start_state.
*/
因此,实际上,它可以只允许使用boost::make_iterator_range(a,b)
调用,其中boost range对象本身是临时的(绑定到常量&
),迭代器仍然是可变的
离题: 如果您确实想将其作为const传递,下面是您的方法(当然,由于上述原因,它不会编译): 甚至
integrate_adaptive(make_controlled(1E-10, 1E-10, stepper_type()), sys,
state_type { 0.0, 0.0 }, 0.0, 11.0, 0.1/*, observer*/);
国家本身将发生变异。因此,传递一个根本不能变异的实常量对象,就不能传递给集成自适应对象。const重载用于允许在integrate_adaptive调用中创建状态。正如sehe已经提到的,可以称为集成自适应
integrate_adaptive( stepper , system , make_pair( a.begin() , a.begin() + 3 ) , t0 , t1 , dt , obs );
在这里,状态是由三个元素组成的一个范围。如果没有第二个重载,则需要编写
auto r = make_pair( a.begin() , a.begin() + 3 ); // Without auto the type will be really complicated.
integrate_adaptive( stepper , system , r , t0 , t1 , dt , obs );
在C++11之前的版本中,
r
的类型非常复杂,类似于pair:iterator,vector:iterator>
和引入第二个重载简化了很多。大多数步进器do\u step
方法也有类似的重载。Addstate\u type const cx=x
然后在调用integrade\u adaptive
时使用cx
而不是x
。您还可以通过调用vec::fixex
构造函数直接初始化cx
。。。类似于状态类型常量cx(0.0,0.0)代码>“结果显示”。坦率地说,我没有看到任何结果,我不认为这是因为我做错了。你指的是什么结果?@sehecoutSeemsconst_cast(x)
导致了这么多错误:在“_oistd::_oicopy_move_a(_II,_II,_OI)[with bool _IsMove=false;_II=const double*;_OI=const double*]的实例化中,除了大量其他错误外,这是行不通的。集成自适应修改状态。无需担心;我手头没有detail::integrate_adaptive()声明,但如果它也不能接受常量引用,至少编译器会告诉您!:-)找到了更多证据表明重载确实是用于逻辑可变状态包装器(包装器是const&
temporaries)非常有帮助的解释感谢确认我的想法。看起来你对图书馆有一些经验(我没有),所以这是一个受欢迎的确认+1@seheheadmyshoulder似乎是这个图书馆的作者。是的,我是作者之一:)
integrate_adaptive(make_controlled(1E-10, 1E-10, stepper_type()), sys,
state_type { 0.0, 0.0 }, 0.0, 11.0, 0.1/*, observer*/);
integrate_adaptive( stepper , system , make_pair( a.begin() , a.begin() + 3 ) , t0 , t1 , dt , obs );
auto r = make_pair( a.begin() , a.begin() + 3 ); // Without auto the type will be really complicated.
integrate_adaptive( stepper , system , r , t0 , t1 , dt , obs );