我正在用特征值进行一些测试(作为我当前使用的增强矩阵的替换),并且当我试图为特征矩阵上方的类定义 CTOR 时, / strong>我遇到了一段代码问题,该代码会产生很多嘈杂的警告。问题显然来自标量类型与标量类型上的指针之间的模板类型混乱。
欢迎所有帮助或建议, 谢谢。
我定义以下模板类
template<typename T>
class CVector : public Eigen::Matrix<T, Eigen::Dynamic, 1, Eigen::StorageOptions::AutoAlign>
{
public:
typedef typename Eigen::Matrix<T, Eigen::Dynamic, 1, Eigen::StorageOptions::AutoAlign> Base_Vector;
.....
我使用Eigen文档提供的代码从Eigen对象和3个构造函数下面的几行进行构造
CVector(size_t size1) : Base_Vector(size1)
{}
CVector(size_t size1, T val): Base_Vector(size1)
{
this->setConstant(5);
}
CVector(T* val_array, size_t val_array_size): Base_Vector(val_array_size)
{
std::copy(val_array, val_array+val_array_size, this->data());
}
但是,当我尝试通过编写如下内容来使用它时,最后的CTOR引起了很多警告:
int tab [] = { 1,2,3,4,5 };
CVector<int> v3(tab, 5);
从VS'2015我得到:
warning C4267: 'argument': conversion from 'size_t' to 'const int', possible loss of data
note: see reference to class template instantiation 'Eigen::internal::is_convertible_impl<unsigned __int64,int>' being compiled
note: see reference to class template instantiation 'Eigen::internal::is_convertible<std::T,int>' being compiled
with
[
T=std::size_t
]
note: see reference to function template instantiation 'Eigen::Matrix<int,-1,1,0,-1,1>::Matrix<std::size_t>(const T &)' being compiled
with
[
T=std::size_t
]
note: see reference to function template instantiation 'Eigen::Matrix<int,-1,1,0,-1,1>::Matrix<std::size_t>(const T &)' being compiled
with
[
T=std::size_t
]
note: while compiling class template member function 'CVector<int>::CVector(T *,std::size_t)'
with
[
T=int
]
note: see reference to function template instantiation 'CVector<int>::CVector(T *,std::size_t)' being compiled
with
[
T=int
]
note: see reference to class template instantiation 'CVector<int>' being compiled
但是另一方面,我使用时完全没有警告
float tab [] = { 1,2,3,4,5 };
CVector<float> v3(tab, 5);
答案 0 :(得分:2)
Eigen使用带符号的类型来存储大小和进行索引。此类型为Eigen::Index
,默认情况下为std::ptr_diff
的typedef。只需将size_t
替换为Eigen::Index
,并在执行此操作的同时,也可以用以下代码替换构造函数的实现:
CVector(Eigen::Index size1) : Base_Vector(size1) {}
CVector(Eigen::Index size1, T val)
: Base_Vector(Base_Vector::Constant(size1, val) { }
CVector(T const * val_array, Eigen::Index val_array_size)
: Base_Vector(Base_Vector::Map(val_array, val_array_size) { }
顺便说一句:不知道,为什么CVector<float> v3(tab, 5);
没有发出与int
变体相同的警告...