Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 强制转换到相同基础类型时的特征行为_C++_Casting_Eigen3 - Fatal编程技术网

C++ 强制转换到相同基础类型时的特征行为

C++ 强制转换到相同基础类型时的特征行为,c++,casting,eigen3,C++,Casting,Eigen3,我有一个模板类,MyClass,它的成员变量类型为Eigen::Matrix,其中X是MyClass特定实例的本机模板参数 假设上述类型的私有变量是myVar。为了使setMyVar函数通用,我将其定义为一个模板函数,如 template <typename U> void setMyVar(const Eigen::Matrix<U, 3, 1>& newVal) const { // myVar is of type Eigen::Matr

我有一个模板类,
MyClass
,它的成员变量类型为
Eigen::Matrix
,其中
X
MyClass
特定实例的本机模板参数

假设上述类型的私有变量是
myVar
。为了使
setMyVar
函数通用,我将其定义为一个模板函数,如

  template <typename U>
  void setMyVar(const Eigen::Matrix<U, 3, 1>& newVal) const { 
     // myVar is of type Eigen::Matrix<T, 3, 1>
     myVar = newVal.cast<T>();
  }

模板
void setMyVar(常数本征::矩阵和newVal)常数{
//myVar的类型为Eigen::Matrix
myVar=newVal.cast();
}
我的问题是,如果
T==U
,Eigen是否会跳过转换操作


我特别感兴趣的是在这种情况下学习Eigen的行为。否则,我非常清楚其他解决方案,例如使用SFINAE强制编译器从
setMyVar
的正确重载中进行选择,或者如果constexp(std::is__相同\U vWhat是模板变量?我看不到typename T。如果
U
T
相同,
cast
是NOP(即,仅返回对原始对象的引用)。它在源代码中有点分散。可能重要的部分在这里:嗯。如果输入和输出标量类型相同,我假设写
typedef typename conditional::type;
的行通过将返回类型设置为
XprType
来揭示意图。谢谢@chtz@ozlsn如果你找到了一个好的答案,并想帮助未来的r埃德斯,欢迎你为自己的问题写一个答案