I have simple code:
#include <iostream>
template<class... Types>
class A;
template<>
class A<> {
public:
explicit A() {
std::cout << "empty" << std::endl;
}
};
template<class Head, class... Tail>
class A<Head, Tail...> {
public:
explicit A(Head head, Tail ...tail) : tail_(tail...) {
std::cout << head << std::endl;
}
private:
A<Tail...> tail_;
};
int main() {
auto a = A(1, 2);
return 0;
}
It doesn't compile:
test2.cpp:27:20: error: class template argument deduction failed:
auto a = A(1, 2);
^
test2.cpp:27: confused by earlier errors, bailing out
It could be fixed by adding deduction guide like:
template<class... T>
A(T...) -> A<T...>;
My question is why compiler can't resolve such simple case?
Implicit deduction guides are only generated from the constructors of the primary class template. (C++17 [over.match.class.deduct]/1.1) You have not defined the primary class template.
Therefore, the class template partial specialization template<class Head, class... Tail> class A<Head, Tail...> ... is ignored when performing class template argument deduction. Writing an explicit deduction guide is a possible solution to this problem, as you have observed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With