12

I'm getting a discrepancy between Clang, GCC and MSVC, where GCC and Clang do what I expect:

#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);
}

MSVC won't compile that but GCC and Clang will.

Is this an MSVC bug? MSVC says:

<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

But if I try other orders of template arguments (as indicated by "OK" and "Bad" comments), it accepts some orders.

cafce25
  • 15,907
  • 4
  • 25
  • 31
Ben
  • 9,184
  • 1
  • 43
  • 56
  • 2
    Your code should be fine. From working with MSVC and templates I _feel_ (without being able to back this up) that MSVC has many more bugs or non-conforming behaviour than Clang and GCC – chrysante May 22 '23 at 18:45
  • 2
    The latest MSVC accepts all your "bad" constructs - but not when using Eigen. [Example](https://godbolt.org/z/dq7KdeMKn) – Ted Lyngmo May 22 '23 at 18:49
  • 3
    The actual `Eigen::Matrix` template is [`template class Eigen::Matrix`](https://eigen.tuxfamily.org/dox/classEigen_1_1Matrix.html). This seems to be a reason of the issue. https://godbolt.org/z/Pa5nefGv9 – 273K May 22 '23 at 19:02
  • 1
    I figured out that the Eigen release included in MSVCs `vcpkg 2023.02.24` (as it says in compiler explorer) is 3.4 and selected that version to test gcc and clang too and they still succeed in compiling it. – Ted Lyngmo May 22 '23 at 19:51

1 Answers1

6

This is a bug in MSVC. I have filed a bug report to Microsoft.

Its was closed as a duplicate which is expected to be fixed in VS 2022 v17.7 (MSVC 19.36).

Thanks to Ben for discovering and to Ted Lyngmo and 273K for analyzing it in the comments!

Benjamin Buch
  • 4,752
  • 7
  • 28
  • 51
  • 1
    This bug was closed as a duplicate of another one: https://developercommunity.visualstudio.com/t/Template-argument-deduction-with-default/10283377 (expected to be fixed in VS 2022 v17.7) – Fedor May 24 '23 at 07:50