获取已在模板中延迟的类型
Get type that already was deferred in template
我创建了一个模板化类,它工作正常:
(我正在使用 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?
只需将type
成员添加到您的MatFactory
:
template <typename T, T V>
class MatFactory {
public:
typedef T type;
...
};
请注意,非类型模板参数相当有限。特别是不允许使用浮点类型(考虑到这一点,这可能已通过 C++2011) 进行了更改)。
如果您知道数组元素类型(可以使用方法 Mat::type() 检索),则可以访问二维数组的元素 M_{ij}
:
M.at<double>(i,j) += 1.f;
假设 M 是双精度浮点数组。对于不同数量的维度,该方法有几种变体。
http://docs.opencv.org/modules/core/doc/basic_structures.html#mat
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 提升的返回类型是什么::apply_visitor(延迟版本)
- 使用延迟加载加载正确的库 - 返回值类型错误
- 延迟评估模板类型函数
- 获取已在模板中延迟的类型
- 基于静态条件延迟对非嵌套类型的引用,且没有编译错误
- 自定义类型的运算符优先级,以及当存在多个相同运算符时是否可能进行延迟求值
- 延迟成员变量类型定义,直到另一个类继承它