4

Boost ライブラリの次のコードでは、次のようになります。

template<class T , class Enabler = void >
struct get_unit_value_impl
{
    static T value(const T &t)
    {
        return t;
    }
    typedef T result_type;
};

...

template<class T>
typename detail::get_unit_value_impl<T>::result_type get_unit_value(const T &t)
{
    return detail::get_unit_value_impl<T>::value(t);
}

の役割はget_unit_value私には不明です。それは何をするためのものか?何かを渡すと、同じ値が返されます。なぜ誰かがそれを構造にラップする必要があるのですか? 実行時間を遅くする以外に何かしますか?

このコードはここから呼び出されます:

template< class Fac1 = double >
struct rel_error
{
    const Fac1 m_eps_abs , m_eps_rel , m_a_x , m_a_dxdt;

    rel_error( Fac1 eps_abs , Fac1 eps_rel , Fac1 a_x , Fac1 a_dxdt )
    : m_eps_abs( eps_abs ) , m_eps_rel( eps_rel ) , m_a_x( a_x ) , m_a_dxdt( a_dxdt ) { }


    template< class T1 , class T2 , class T3 >
    void operator()( T3 &t3 , const T1 &t1 , const T2 &t2 ) const
    {
        using std::abs;
        set_unit_value( t3 , abs( get_unit_value( t3 ) ) / ( m_eps_abs + m_eps_rel * ( m_a_x * abs( get_unit_value( t1 ) ) + m_a_dxdt * abs( get_unit_value( t2 ) ) ) ) );
    }

    typedef void result_type;
};
4

1 に答える 1

2

スペシャライゼーションの 1 つをコピーするのを忘れました:

template<class T , class Enabler = void >
struct get_unit_value_impl
{
    static T value(const T &t)
    {
        return t;
    }
    typedef T result_type;
};

#ifndef __CUDACC__
template<class Unit , class T>
struct get_unit_value_impl< boost::units::quantity< Unit , T> >
{
    static T value( const boost::units::quantity< Unit , T> &t )
    {
        return t.value();
    }
    typedef T result_type;
};
#endif

ポイントget_unit_value()は、値を取り込んでそれを返すか、または a から値を返すboost::unit::quantity<Unit, T>ことです。tそうすれば、呼び出し元は if がただのintか、もっと複雑かどうかを心配する必要がなくなります。

int x = 7;
quantity<length> L = 2.0*meters;

get_unit_value(x); // 7
get_unit_value(L); // 2.0
于 2015-02-10T12:09:54.930 に答える