Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 调用另一个函数重载_C++_Templates_Boost_Overloading_Odeint - Fatal编程技术网

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&)
。此处的文件说明(除其他外)

  • \param
    系统需要解决的系统功能,因此是ODE的r.h.s。它必须满足
    简单系统
    的概念
  • \param
    x应解决的ODE状态如果 该步骤成功。可以是一个助推范围
我只是不认为你想要一个稳定的状态。因为如果状态实际上是常量,函数就不可能实现它的承诺

我假设重载的存在是为了容纳表示可变状态的状态对象,即使
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
方法也有类似的重载。

Add
state\u type const cx=x
然后在调用
integrade\u adaptive
时使用
cx
而不是
x
。您还可以通过调用
vec::fixex
构造函数直接初始化
cx
。。。类似于
状态类型常量cx(0.0,0.0)“结果显示”。坦率地说,我没有看到任何结果,我不认为这是因为我做错了。你指的是什么结果?@sehe
coutSeems
const_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 );