Ben*_*Ben 12 c++ language-lawyer template-argument-deduction
我发现 Clang、GCC 和 MSVC 之间存在差异,其中 GCC 和 Clang 执行了我所期望的操作:
#include <Eigen/Core>
template <typename Indices, typename T, int Rows> //< Bad
//template <typename Indices, int Rows, typename T> //< OK
//template <typename T, int Rows, typename Indices> //< OK
//template <typename T, typename Indices, int Rows> //< Bad
//template <int Rows, typename Indices, typename T> //< Bad
//template <int Rows, typename T, typename Indices> //< Bad
void f(
const Eigen::Matrix<T, Rows, 1>&,
const Indices&
) {}
int main() {
f(Eigen::Matrix<double, 6, 1>{}, 1);
}
Run Code Online (Sandbox Code Playgroud)
这是 MSVC 错误吗?MSVC 说:
<source>(16): error C2672: 'f': no matching overloaded function found
<source>(9): note: could be 'void f(const Eigen::Matrix<T,Rows,1,0|_Rows==1&&false?Eigen::RowMajor:true&&_Rows!=1?Eigen::ColMajor:Eigen::ColMajor,_Rows,1> &,const Indices &)'
<source>(16): note: 'void f(const Eigen::Matrix<T,Rows,1,0|_Rows==1&&false?Eigen::RowMajor:true&&_Rows!=1?Eigen::ColMajor:Eigen::ColMajor,_Rows,1> &,const Indices &)': could not deduce template argument for 'const Eigen::Matrix<T,Rows,1,0|_Rows==1&&false?Eigen::RowMajor:true&&_Rows!=1?Eigen::ColMajor:Eigen::ColMajor,_Rows,1> &' from 'Eigen::Matrix<double,6,1,0,6,1>'
Compiler returned: 2
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试其他顺序的模板参数(如“OK”和“Bad”注释所示),它会接受一些订单。