Boost logo

Ublas :

Subject: [ublas] Tag dispatching
From: Marco Guazzone (marco.guazzone_at_[hidden])
Date: 2009-09-11 06:29:56


Hello dears,

I've noticed that in uBlas tag dispatching is done via the creation of
a temporary object. E.g.:

boost::numeric::ublas::leading_dimension(m, typename
M::orientation_category() );
boost::numeric::ublas::end (it1_, iterator1_tag ())

Is there a specific why this method (dispatch-by-instance) is better
than using a dipatch-by-type?

In general, dispatch-by-instance is preferable if tags may have a
hierarchy so that this belowe works:
struct base_tag1 {};
struct base_tag2 {};
struct derived_tag: public base_tag {};

void foo(/*...*/, my_base_tag1 const&); // #1
void foo(/*...*/, my_base_tag2 const&); // #2

foo(/*...*/, base_tag1()); // Call #1
foo(/*...*/, derived_tag1()); // Call #2

However, these creates temporary objects.

Instead, using dispatch-by-type we avoid to create temp objects even
if we loose the possibility to use tags hierarchy.

So returning to uBlas, instead of using:
---[code]---
    template < typename M >
    BOOST_UBLAS_INLINE
    int leading_dimension( const M &m, row_major_tag ) {
        return m.size2() ;
    }
    template < typename M >
    BOOST_UBLAS_INLINE
    int leading_dimension( const M &m, column_major_tag ) {
        return m.size1() ;
    }
    template < typename M >
    BOOST_UBLAS_INLINE
    int leading_dimension( const M &m ) {
        return leading_dimension( m, typename M::orientation_category() ) ;
    }
---[/code]---

Why not use the following (for instance)?
---[code]---
    template struct<typename O>
    struct leading_dimension_impl;

    template <>
    leading_dimension_impl<row_major_tag>
    {
      template <typename M>
      static BOOST_UBLAS_INLINE typename int apply(M const& m) {
return m.size2(); }
    };
    template <>
    leading_dimension_impl<column_major_tag>
    {
      template <typename M>
      static BOOST_UBLAS_INLINE typename int apply(M const& m) {
return m.size1(); }
    };

    template < typename M >
    BOOST_UBLAS_INLINE
    int leading_dimension( const M &m ) {
        return leading_dimension<typename M::orientation_category>::apply(m) ;
    }
---[/code]---

I think in uBlas tag hierarchy is not needed.

Note: I'm a beginner of template meta-programming so sorry if the
question is trivial.

Thank you!!

Cheers,

-- Marco