c ++模板类成员函数的部分特化

时间:2014-06-04 15:09:18

标签: c++ templates partial-specialization

似乎确实存在一些密切相关的问题,但我正在努力研究如何应用他们的解决方案。

我有一个traits类,如下所示,用于操作我使用boost::numeric:ublas::matrix的矩阵(以及其他矩阵实现)。我想部分地专注于注释中显示的switch_storage_order,但是由于函数不能部分专门化而失败。

我不想部分专门化matrix_traits结构,因为这需要重新定义其所有成员的开销。一种解决方案是将每个与矩阵相关的函数分离到它自己的结构中,但将它们分组在一个traits类中会很好。

有什么想法吗?随意评论特征概念的一般应用。

#include <boost/numeric/ublas/matrix.hpp>

enum matrix_storage_order {row_major, column_major};

template<class matrix_type>
struct matrix_traits {
  // Default expects c-style row_major storage order.
  static matrix_storage_order get_storage_order(const matrix_type& m)
  { return row_major; }

  // By default can't change storage order so simply transpose.
  static void switch_storage_order(matrix_type& m) { m.transpose(); }
};

namespace ublas = boost::numeric::ublas;

/* This doesn't work with error C2244:
  * 'matrix_traits<matrix_type>::switch_storage_order' : unable to match function
  * definition to an existing declaration
  */
// template<class value_type>
// void matrix_traits<ublas::matrix<value_type> >::switch_storage_order(
//     ublas::matrix<value_type>& m) {
//   m = boost::numeric::ublas::trans(m);
// }

typedef boost::numeric::ublas::matrix<double> matrix_double;

template<>
void matrix_traits<matrix_double>::switch_storage_order(matrix_double& m) {
  m = boost::numeric::ublas::trans(m);
}

template <class matrix_type>
void function_requiring_column_major_storage_order(matrix_type& m) {
  bool switch_order =
    matrix_traits<matrix_type>::get_storage_order(m) == row_major;
  if (switch_order) matrix_traits<matrix_type>::switch_storage_order(m);
  // ... Do some work on m.
  if (switch_order) matrix_traits<matrix_type>::switch_storage_order(m);
}

int main() {
  matrix_double m;
  // ... Fill the matrix.
  function_requiring_column_major_storage_order(m);
}

2 个答案:

答案 0 :(得分:2)

如果您可以更改static void switch_storage_order(matrix_type& m)的实施,则可以使用以下内容:

// By default can't change storage order so simply transpose.
static void switch_storage_order(matrix_type& m) { transposer<matrix_type>()(m); }

// generic case
template <typename T>
struct transposer {
    void opearator () (T& m) const { m.transpose(); }
};

// your specialization.
template<typename T>
struct transposer<ublas::matrix<T>> {
    void opearator () (ublas::matrix<T>& m) const { m = boost::numeric::ublas::trans(m); }
};

答案 1 :(得分:1)

当我想(但不能)部分专门化模板功能时,我最终做的就是根本不专门化模板功能,只需简单地将其转发到实际工作中到内部帮助器模板类/结构的静态函数。然后我继续并部分专门化该模板类/结构。

给你一个例子(假设我想在B是bool的情况下部分专门化doStuff()):

namespace detail
{
    // primary template ... implement general case here
    template < typename A, typename B >
    struct DoStuffImpl
    {
        inline static void impl( A a, B b )
        {
            // ...
        }
    };

    // partial specialization for < A, bool >
    template < typename A >
    struct DoStuffImpl< A, bool >
    {
        inline static void impl( A a, bool b )
        {
            // ...
        }
    };
}

template < typename A, typename B >
void doStuff( A a, B b )
{
    detail::DoStuffImpl< A, B>::impl( a, b );
}
相关问题