c++模板类成员函数的局部特化
c++ partial specialization of template class member functions
似乎有几个密切相关的问题,但我正在努力找出如何应用他们的解决方案。
我有一个trait类,如下所示,用于操作我与boost::numeric:ublas::matrix
(以及其他矩阵实现)一起使用的矩阵。我想部分专门化switch_storage_order
,如评论中所示,但是这失败了,因为函数不能部分专门化。
我不想部分专门化matrix_traits
结构体,因为这需要重新定义其所有成员的开销。一种解决方案是将每个与矩阵相关的函数分离到自己的结构中,但最好将它们分组在一个trait类中。
任何想法?请随意评论特征概念的一般应用。
#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);
}
如果您可以更改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); }
};
当我想(但不能)部分专门化模板函数时,我最终做的是根本不专门化模板函数,而是简单地将其实际工作转发给内部helper模板类/结构的静态函数。然后我继续对模板类/结构进行部分特化。
给你一个例子(假设我想部分特化doStuff()对于B是bool的情况):
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 );
}
相关文章:
- 局部变量保留函数中的值
- 为什么 'main' 函数中的局部 int 变量会自动初始化?
- 如何使用 C++ 中的继承函数访问派生类中的局部变量
- 如何在函数外部访问函数中局部变量的值?
- 赋予全局变量而不是局部变量优先级的函数 - (异常行为)
- 获取具有静态局部变量的绑定/推断捕获 lambda 的函数指针
- 函数局部静态变量:从性能角度来看的优点/缺点
- 是否可以影响 C++ 中回调函数的局部变量?
- 将共享指针传递给函数参数 - 将其分配给局部变量的正确方法是什么
- 调用一个小函数两次(例如在if条件和主体中)比将结果存储在局部变量中更可取
- C++ - 指向函数中局部变量的指针
- 如何在 C++ 的 lambda 函数中传递同名的局部变量和参数(使用此关键字)?
- Clang++ 6.0 内存清理器未报告返回值指示条件分支的函数中的未初始化局部变量
- 如何将局部变量传递给 lambda 函数
- 局部变量的作用域是块或函数
- 将局部函数作为参数传递给全局函数
- 正在返回指向局部函数变量的指针
- 这个静态变量赋值给局部函数中的指针有什么问题
- 可以从局部函数调用类的私有函数吗
- C++静态局部函数与全局函数