【问题标题】:Calling another function overload调用另一个函数重载
【发布时间】:2015-01-13 21:54:23
【问题描述】:

我要从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。然后我从这个测试文件中调用它们:

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 库的机制,请告诉我第二个函数的优势。

更新:

原代码:github,doc

【问题讨论】:

  • 添加state_type const cx = x;,然后在对integrate_adaptive 的调用中使用cx 而不是x。您还可以通过调用vec::fixex&lt;2&gt; 构造函数来直接初始化cx ...类似于state_type const cx(0.0, 0.0);
  • “结果显示”。坦率地说,我没有看到任何结果,我不认为那是因为我做错了。你指的是什么结果?
  • @sehe cout&lt;&lt;"type one"&lt;&lt;endl; //added by me

标签: c++ templates boost overloading odeint


【解决方案1】:

状态本身将发生变异。所以传递一个根本不能变异的真正的 const 对象是不能传递到集成自适应的。这里的 const 重载是为了允许在集成自适应的调用中创建状态。正如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&lt; vector&lt; double &gt;::iterator , vector&lt; double &gt;::iterator &gt;,引入第二个重载简化了很多。大多数步进器do_step 方法也有类似的重载。

【讨论】:

  • 感谢您对我的想法的确认。看起来您对图书馆有一些经验(我没有),所以这是一个受欢迎的确认。 +1
  • @sehe headmyshould 似乎是这个库的作者。
  • 是的,我是作者之一 :)
【解决方案2】:

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

integrate_adaptive 最终从 controlled_step_result try_step(System, const StateInOut&amp;x, time_type&amp;, time_type&amp;) 调用 odeint::copy(来自 odeint/util/copy.hpp)。此处的文档说明(除其他外)

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

我根本不认为你想要一个 const 状态。因为如果状态实际上是 const,则该函数不可能实现它的承诺。

我假设存在重载以容纳表示可变状态的状态对象,即使在 const 时也是如此。这看起来很奇怪,但如果您愿意,可以使用double * const&amp;(而不是double const* const&amp;)来想象。

更新

文档说 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&amp;)并且迭代器仍然是可变的。


题外话:

如果您确实想将它作为 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*/);

【讨论】:

  • 发现更多证据表明重载确实适用于逻辑可变状态包装器(其中包装器是 const&amp; 临时)
  • 非常有帮助的解释
【解决方案3】:

您只需在调用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 的类型更改为其他类,可以更快地发现自己。)

【讨论】:

  • 似乎const_cast&lt;const state_type &amp;&gt;(x) 导致了很多错误:In instantiation of ‘_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = const double*; _OI = const double*] 以及大量其他错误
  • 是的,这行不通。 integration_adaptive 修改状态。
  • 不用担心;我手边没有 detail::integrate_adaptive() 的声明,但如果它也不能接受 const 引用,至少编译器会告诉你! :-)
猜你喜欢
  • 2016-07-03
  • 2017-03-07
  • 2020-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多