C++17 offers class template argument deduction.
I have the following small example (you can paste into onlinegdb with C++17 enabled, no problem) where it fails under certain circumstances but I do not know why:
#include <iostream>
enum class Res{
ResA,
ResB
};
template<Res>
class B{
};
template<Res T>
class A{
//If I remove this construtor, template type deduction will not work anymore
public:
A(B<T> b){
}
};
template<>
class A<Res::ResA>{
public:
A(B<Res::ResA> b){
std::cout<<"A res A\n";
}
};
int main()
{
B<Res::ResA> b;
A a(b);
}
The code above works. But as soon as I change A
's constructor to be the any other constructor than in the template specializations, the template argument deduction will not work and A
has to be initialized by A<Res::ResA>
.
I am at loss. Why could this be the case? Thank you for any ideas!
Res::ResA
if it's not deducable. If you would add another template specializationA<Res::ResB>
that takes anB<Res::ResB>
in it's constructor it wouldn't work since there is not actual deduction involved. Only a default value. – super