c++ - Trailing class template arguments not deduced -
the code below fails compile gcc 7.1.0, complains providing wrong number of template arguments in second line of main. version of gcc supposed implement template argument deduction of class templates.
i think compiler should able deduce class template argument t2 bar, means shouldn't have explicitly specify both arguments (bar<int, int>
) given paragraph 17.8.1.3 of c++17 draft says, "trailing template arguments can deduced (17.8.2) or obtained default template-arguments may omitted list of explicit template-arguments."
am wrong? compiler wrong? oversight or deliberate design?
template <typename t> struct foo { foo(t t) {} }; template <typename t1, typename t2> struct bar { bar(t2 t) {} }; template <typename t1, typename t2> void bar(t2 t) {} int main(int argc, char **argv) { foo(42); // works bar<int>(42); // fails compile "wrong number of // template arguments (1, should 2)" bar<int>(42); // works }
this expected behavior; unlike template argument deduction (for function templates), class template argument deduction (since c++17) works when no template arguments provided.
class template argument deduction performed if no template arguments provided. if @ least 1 argument specified, deduction not take place.
std::tuple t(1, 2, 3); // ok: deduction std::tuple<int,int,int> t(1, 2, 3); // ok: arguments provided std::tuple<int> t(1, 2, 3); // error: partial deduction
that means example can't take advantage of class template argument deduction , have specify template arguments. if want class template argument deduction taking effect have specify none, template parameter t1
can't deduced.
on other hand, following code work.
template <typename t1, typename t2> struct bar { bar(t1, t2) {} // make possible deduce t1 }; int main(int argc, char **argv) { bar bar(42, 42); // take advantage of class template argument deduction }
Comments
Post a Comment