Reputation: 99
I am learning about class template argument deduction and I could not understand why statement C<> c(5);
fails unlike the call func<>(5)
. I mean I expected that in the statement C<> c(5);
, T should have been deduced to int
but to my surprise the program generated an error saying error: wrong number of template arguments (0, should be 1)
.
template<typename T>
void func(T t)
{
}
template<typename T>
struct C
{
C(T t)
{
}
};
int main()
{
func<>(5); //works as expected with T = int
C<> c(5); //why this fails? I mean why T is not being deduced from the argument 5's type?
C b(5); //works as expected with T = int
}
So I want to know why does the statement C<> c(5);
fail to compile.
Upvotes: 1
Views: 655
Reputation: 1
Because class template argument deduction(CTAD) doesn't work if/when you explicitly use/specify the template argument list <>
even if it is empty!
This can be seen from CTAD's documentation which states:
Notes
Class template argument deduction is only performed if no template argument list is present. If a template argument list is specified, deduction does not take place.
(emphasis mine)
This means that since you've explicitly specified the template argument list <>
, deduction won't happen here.
Upvotes: 1