c++ - 调用另一个函数重载

标签 c++ templates boost overloading odeint

我正准备从 c++ boost 库中了解 odeint,我需要知道哪个部分做了什么。 在 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。然后我从这个测试文件中调用它们:

最小.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

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

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

更新:

原码:github , doc

最佳答案

状态本身会发生变化。所以传递一个真正的 const 对象,它根本不能被改变,不能传递给集成自适应。 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< vector< double >::iterator , vector< double >::iterator >并引入第二个重载简化了很多。大多数步进器do_step方法也有类似的重载。

关于c++ - 调用另一个函数重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27932196/

相关文章:

c++ - pion 网络库 : testing sample webservices

C++:const volatile 方法

c++ - 许多慢核没有加速?

c++ - 使用模板类参数的模板类特化

c++ - 向现有多索引容器添加更多索引

c++ - 使用 boost::gil 从内存中读取 JPEG 图像

c++ - 可以在编译时隐式引用类名吗?

c++ - 模板定义宏

c++ - 如何在 C++ 中调用外部模板函数

c++ - 在这个 boost::asio 应用程序示例中,如何使用 send_break 进行重置?