templates - C++ Partial Specialization Not Working for Differing Size Eigen Matrices -
i've been banging head wall last couple of hours trying figure out why partial template specializations failing, , use help.
basically, i'm working on code relies on knowing sizes of matrices @ compile time, i'm trying generically templates. there seems kind of issue using type compositions don't understand. way of illustration, consider following (slightly pointless) code:
template <typename t> struct type1 { typedef bool bar; }; template <typename t, int r, int c> struct type1<matrix<t, r, c>> { typedef matrix<t, r, c> bar; }; template <typename t1, typename t2> struct type2 { typedef bool bar; }; template <typename t, int r, int m, int c> struct type2<matrix<t, r, m>, matrix<t, m, c>> { typedef matrix<float, 2, 2> bar; }; template <typename t1, typename t2> struct type3 { typedef bool bar; }; template <typename t, int r, int m, int c> struct type3<matrix<t, r, m>, matrix<t, m, c>> { typedef matrix<t, r, c> bar; }; template <typename t> struct test { static bool foo() { return false; } }; template <typename t, int r, int c> struct test<matrix<t, r, c>> { static bool foo() { return true; } };
my understanding of template composition these templates should able composed in ways below every line should return true. note, however, last 2 return false.
/* true <- */ test<matrix<float, 2, 2>>::foo(); /* true <- */ test<matrix<float, 6, 3>>::foo(); /* true <- */ test<type1<matrix<float, 2, 7>>::bar>::foo(); /* , on other size . . . */ /* true <- */ test<type2<matrix<float, 3, 3>, matrix<float, 3, 3>>::bar>::foo(); /* true <- */ test<type3<matrix<float, 2, 2>, matrix<float, 2, 2>>::bar>::foo(); /* , on other pair of square sizes . . . */ /* false <- */ test<type2<matrix<float, 2, 3>, matrix<float, 3, 2>>::bar>::foo(); /* false <- */ test<type3<matrix<float, 2, 4>, matrix<float, 4, 1>>::bar>::foo(); /* , on other pair of non-square sizes . . . */
what's weird when replace eigen::matrix
arbitrary template <typename t, int r, int c> struct thing
works expected (ie. tests return true), why think problem specific eigen matrices.
edit: actually, seems tied fact i'm building on msvc (fails identically on both 2015 , 2017). when compile on either g++ or clang, works fine. more evidence visual c++ real mess (not it's needed).
furthermore, seems tied cases 2 parameters have differing sizes. example:
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; };
this succeeds when first , second matrix types have same size, fails when differ. example:
/* 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();
unfortunately, i'm stuck msvc time being, owing annoying dependencies, need workaround. have ideas?
just make complete, solution suggested @chtz in comments above totally works msvc 2015 , 2017. way of example specific differing size issue question, works perfectly:
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();
Comments
Post a Comment