问题描述
我正在尝试实现一个概念
- 模板化自己
- 可用作编译时接口。
C ++ 20 怎么可能?
示例:
让我们有一个矩阵求反实用程序,它接受分解函子作为“依赖注入”输入。我知道如何使用常规接口(例如,纯虚拟机等)实现此功能,但是我想知道如何使用C ++ 20将其实现为模板参数。
客户端代码(需要反转矩阵的任何内容)应类似于以下内容:
// We have a template class representing a matrix-to-be-inverted
// Number is a concept constraining T to be int,float,double,etc.
template <Number T>
class Matrix;
// Matrix decomposition concept
// ???
template <typename T,template <class> class D>
concept Decomposer = Number<T> && requires( D<T> decomposer,Matrix<T> mat,std::string str )
{
{decomposer(mat)} -> void;
{decomposer(str)} -> Matrix<T>;
}; // ???
// The below two classes should follow the same interface,which is
// specified by the concept. Both Eigen~ and SVDDecomposer output three matrices,// which can be called,for example,"first","second" and "third"
template <Number T>
class EigenDecomposer
{
public:
void operator()(const Matrix<T> &input){ // implementation }
Matrix<T> get_output(std::string output_type) {} // get some output using keywords that are common across implementations
};
template <Number T>
class SVDDecomposer
{
public:
void operator()(const Matrix<T> &input){ // implementation }
Matrix<T> get_output(std::string output_type) {
};
// So far,so good. Now comes the interesting (for me) part which won't work:
template <Number T,Decomposition<T> D>
class MatrixInversion
{
public:
Matrix<T> operator()(const Matrix<T> &input)
{
// use the templated Decomposition<T> to make the decomposition
D<T> decomposition;
decomposition(input);
// ...
}
}
这个想法是客户端指定反转的数字类型(int,float,...)和分解器(也模板化为数字类型)。客户端不关心Inversion的内部,只关心数字类型和分解有效(应由编译器确定,而不是在运行时确定)。
- 我应如何实施分解器概念?
- 我应该如何指定
MatrixInversion
的模板,以便适合分解器概念?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)