我创建了一个模板类,它工作正常:
(我正在使用 openCV 库,所以我有 cv::Mat 类型的矩阵)
template < class T, T V >
class MatFactory
{
private:
const T static VALUE = V;
public:
void create(/* some args */)
{
cv::Mat M;
// filling the matrice with values V of type T
// loop i,j
M.at<T>(i,j) = V;
return M;
}
};
但稍后在代码中,我需要在某些索引 (i,j) 处获取矩阵 M 的元素。但是我怎么知道 T 型呢?
MatFactory<int, 1> MF;
// getting object
cv::Mat m = MF.create();
// then I need to get some value (with `.at<>` method)
int x = m.at<int>(2,3);
// But, it means I should every time explicitly declare INT type
// Can I somehow get the type, that was passed to template factory
// Than i'll write something like this:
T x = m.at<T>(2,3);
// How to get T from the deferred template?