ren*_*ond 5 c++ templates typedef boost-ublas
我正在使用ublas作为我的矩阵代码,但我希望它可以交换,所以我这样做:
typedef boost::numeric::ublas::matrix<double> cMatrix;
Run Code Online (Sandbox Code Playgroud)
今天,我需要将一些矩阵更改为有界大小,所以我也会这样:
typedef boost::numeric::ublas::bounded_matrix<double, 3, 3> Matrix3d;
Run Code Online (Sandbox Code Playgroud)
问题是我的旧函数声明:
void cClass::function(int param1,
int param2,
cMatrix ¶m3,
int param4);
Run Code Online (Sandbox Code Playgroud)
不再有效.它给了我:
error : a reference of type "cMatrix &" (not const-qualified) cannot be initialized with a value of type "Matrix3d"
Run Code Online (Sandbox Code Playgroud)
我设法通过将声明更改为:
template <class A>
void cClass::function(int param1,
int param2,
boost::numeric::ublas::matrix<double, boost::numeric::ublas::row_major, A> ¶m3,
int param4);
Run Code Online (Sandbox Code Playgroud)
问题是我的定义是一个cpp文件,所以我必须在cpp中做这样的事情:
void dummyFunc()
{
cClass dummy(NULL, NULL);
cMatrix c;
Matrix12d d12;
dummy.function(-1, -1, c, -1);
dummy.function(-1, -1, d12, -1);
}
Run Code Online (Sandbox Code Playgroud)
有没有办法避免使用dummyFunc或以其他方式概括函数?
如果两者matrix具有bounded_matrix完全相同的 API,您可以抽象类型并cClass::function使用可变参数模板进行泛化:
template<template <class...> class M, class... Arg>
void cClass::function(int param1,
int param2,
M<Arg...> ¶m3,
int param4);
Run Code Online (Sandbox Code Playgroud)