Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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++;部分特化不适用于不同大小的特征矩阵_C++_Templates_Matrix_Metaprogramming_Eigen - Fatal编程技术网

C++ C++;部分特化不适用于不同大小的特征矩阵

C++ C++;部分特化不适用于不同大小的特征矩阵,c++,templates,matrix,metaprogramming,eigen,C++,Templates,Matrix,Metaprogramming,Eigen,在过去的几个小时里,我一直在绞尽脑汁想弄明白为什么部分模板专业化失败了,我真的需要一些帮助 基本上,我正在编写一些代码,这些代码依赖于在编译时知道矩阵的大小,但我尝试使用模板来完成。使用类型组合似乎存在某种问题,我真的不明白。通过举例说明,考虑下面的(稍微没有意义的)代码: 当第一个和第二个矩阵类型的大小相同时,此操作成功,但当它们不同时,此操作失败。例如: template <typename T1, typename T2> struct typetest { typedef b

在过去的几个小时里,我一直在绞尽脑汁想弄明白为什么部分模板专业化失败了,我真的需要一些帮助

基本上,我正在编写一些代码,这些代码依赖于在编译时知道矩阵的大小,但我尝试使用模板来完成。使用类型组合似乎存在某种问题,我真的不明白。通过举例说明,考虑下面的(稍微没有意义的)代码:

当第一个和第二个矩阵类型的大小相同时,此操作成功,但当它们不同时,此操作失败。例如:

template <typename T1, typename T2> struct typetest { typedef bool bar; };
template <typename T, int R1, int C1, int R2, int C2> struct typetest<Matrix<T, R1, C1>, Matrix<T, R2, C2>> { typedef Matrix<float, 2, 2> bar; };
/* true  <- */  Test<typetest<Matrix<float, 2, 3>, Matrix<float, 2, 3>>::bar>::foo();
/* true  <- */  Test<typetest<Matrix<float, 6, 1>, Matrix<float, 6, 1>>::bar>::foo();
/* false <- */  Test<typetest<Matrix<float, 2, 2>, Matrix<float, 2, 3>>::bar>::foo();
/* false <- */  Test<typetest<Matrix<float, 5, 3>, Matrix<float, 2, 4>>::bar>::foo();

/*true为了完整起见,@chtz在上述评论中建议的解决方案完全适用于MSVC 2015和2017。作为问题中具体不同尺寸问题的示例,这一点非常有效:

template <typename T1, typename T2> struct typen { typedef bool bar; };
template <typename T, int R, int M, int C, int O1, int O2, int Rc1, int Rc2, int Cc1, int Cc2>
    struct typen<Matrix<T, R, M, O1, Rc1, Cc1>, Matrix<T, M, C, O2, Rc2, Cc2>> { typedef Matrix<T, R, C> bar; };
...
/* true :-) */  Test<typen<Matrix<float, 2, 3>, Matrix<float, 3, 4>>::bar>::foo();
模板结构类型n{typedef bool bar;};
模板
结构类型n{typedef矩阵条;};
...
/*true:-)*/Test::foo();

对于Eigen::Matrix,一切都适合我(没有假值)。我可以给出一个屏幕截图。顺便提一下,我使用了最新的3.3.4。错误消息?已经报告了,作为一个解决办法,您是否尝试过
模板结构测试{…
,即添加
特征::矩阵的可选/隐式模板参数
?(我没有MSVC,因此无法测试)
/* true  <- */  Test<typetest<Matrix<float, 2, 3>, Matrix<float, 2, 3>>::bar>::foo();
/* true  <- */  Test<typetest<Matrix<float, 6, 1>, Matrix<float, 6, 1>>::bar>::foo();
/* false <- */  Test<typetest<Matrix<float, 2, 2>, Matrix<float, 2, 3>>::bar>::foo();
/* false <- */  Test<typetest<Matrix<float, 5, 3>, Matrix<float, 2, 4>>::bar>::foo();
template <typename T1, typename T2> struct typen { typedef bool bar; };
template <typename T, int R, int M, int C, int O1, int O2, int Rc1, int Rc2, int Cc1, int Cc2>
    struct typen<Matrix<T, R, M, O1, Rc1, Cc1>, Matrix<T, M, C, O2, Rc2, Cc2>> { typedef Matrix<T, R, C> bar; };
...
/* true :-) */  Test<typen<Matrix<float, 2, 3>, Matrix<float, 3, 4>>::bar>::foo();