Trailing class template arguments not deduced

梦想的初衷 提交于 2019-11-29 06:02:00

This is expected behavior; unlike template argument deduction (for function templates), class template argument deduction (since C++17) only works when no template arguments are provided.

Class template argument deduction is only performed if no template arguments are provided. If at least one argument is specified, deduction does not take place.

std::tuple t(1, 2, 3);              // OK: deduction
std::tuple<int,int,int> t(1, 2, 3); // OK: all arguments are provided
std::tuple<int> t(1, 2, 3);         // Error: partial deduction

That means for your example you can't take advantage of class template argument deduction and have to specify all the template arguments. If you want class template argument deduction taking effect you have to specify none, but the template parameter T1 can't be deduced.

On the other hand, the following code would work.

template <typename T1, typename T2>
struct Bar {
    Bar(T1, T2) {}   // make it possible to deduce T1
};

int main(int argc, char **argv) {
    Bar bar(42, 42); // take advantage of class template argument deduction
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!