调用另一个函数重载

时间:2015-01-13 21:54:23

标签: c++ templates boost overloading odeint

我要从odeint库中了解c++ boost,我需要知道哪个部分可以做什么。 在boost/numeric/odeint/integrate/integrate_adaptive.hpp中,有一个名为integrate_adaptive的函数。此功能有一些重载。我操作的简化文件如下:

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

这两个函数之间的区别与参数start_state有关。在第二个函数中,它包含const关键字。我想知道调用哪个重载。所以,我在每个函数中添加了一个cout。然后我从这个测试文件中调用它们:

minimal.cpp

#include "integrate_adaptive_minimal.hpp"
#include <boost/numeric/odeint.hpp>
#include <armadillo>

using namespace arma;
using namespace boost::numeric::odeint;

typedef vec::fixed<2> state_type;

void sys( const state_type &x , state_type &dxdt , const double t)
{
    mat A;
    A<<-1<<0<<endr<<0<<-1;
    mat B;
    B<<1<<endr<<1;

    dxdt=A*x+B*(t>0?1:0);
}

void observer( const state_type &x , const double t )
{
}

typedef runge_kutta_dopri5<state_type> stepper_type;

int main()
{
    state_type x;
    x(0) = 0.0;
    x(1) = 0.0;
    integrate_adaptive(make_controlled(1E-10,1E-10,stepper_type()),
                        sys,x,0.0,11.0,0.1,observer);
    return 0;
}

当我编译并运行时:

g++ -std=c++11 minimal.cpp  -larmadillo -lboost_thread -lboost_filesystem -lboost_system -Wfatal-errors

./a.out

结果显示第一个函数integrate_adaptive被调用,但没有const。现在,我想知道调用第二个函数的正确方法是什么?对象x不能是const

如果有人甚至知道boost库的机制,请告诉我第二个函数的优点。

更新

原始代码:githubdoc

3 个答案:

答案 0 :(得分:2)

说实话,我对这个问题的前提感到有些困惑。

integrate_adaptive最终从odeint::copy调用controlled_step_result try_step(System, const StateInOut&x, time_type&, time_type&)(来自odeint / util / copy.hpp)。这里的文档陈述(以及其他)

  
      
  • \param system要解决的系统函数,因此r.h.s. ODE。它必须符合Simple System概念。
  •   
  • \param x应该解决的ODE的状态。 覆盖    步骤成功 。可以是提升范围。
  •   

我根本不认为你想要一个常态。因为如果状态实际上是const,那么这个功能就不可能实现它的承诺。

我假设存在过载以容纳表示可变状态的状态对象,即使const也是如此。这看起来很奇怪,但如果你愿意,可以设想double * const&(而不是double const* 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&)并且迭代器仍然是可变的。


非主题:

如果您 希望将其作为常规传递给您,请按照以下方式进行操作(当然,由于上述原因,它无法编译) :

state_type const x { 0, 0 };
integrate_adaptive(make_controlled(1E-10, 1E-10, stepper_type()), sys, x, 0.0, 11.0, 0.1/*, observer*/);

甚至

integrate_adaptive(make_controlled(1E-10, 1E-10, stepper_type()), sys, 
                 state_type { 0.0, 0.0 }, 0.0, 11.0, 0.1/*, observer*/);

答案 1 :(得分:2)

国家本身将发生变异。因此传递一个根本无法变异的真实const对象不能传递给整合自适应。 const重载在这里允许在integrate_adaptive的调用中创建状态。正如已经提到的,可以调用整合自适应

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< vector< double >::iterator , vector< double >::iterator >,引入第二次重载简化了很多。大多数步进器do_step方法也有类似的重载。

答案 2 :(得分:0)

您只需在调用integrate_adaptive()时将x投射为const

integrate_adaptive(make_controlled(1E-10,1E-10,stepper_type()),
                        sys, const_cast<const state_type &>(x),0.0,11.0,0.1,observer);

该函数的const版本承诺它不会在其范围内修改x的内容,但main()可以在integrate_adaptive()返回后继续修改它。

(使用C ++风格const_cast的好处是,如果您决定将x的类型更改为其他类,则可以更快地抓住自己。)

相关问题