On 7/13/2015 2:39 PM, Doug Mika wrote:
> If a template variable type can be deduced from the variables
> provided in the constructor of a template class, then is it needed?
It can't. On what basis do you assume that it can? You ask a question
that is not possible to answer because it is based on a wrong premise.
> I thought it wasn't, but when in the following program (below) I
replace the line:
> map<int, string, function<bool(const int&, const int&)>>
> mapIntToString1(ReverseSort<int>);
> with:
> map<int, string>
> mapIntToString1(ReverseSort<int>);
> the code no longer compiles? Why isn't the compiler able to deduce
from the submitted function ReverseSort<int> that the template variable
type is bool(*)(const int&, const int&)?
It's not required to, by the language Standard. And the requirement is
absent because it would be much more difficult to define other rules if
it existed.
> Is the compiler only smart
enough to deduce this for template functions?
Yes, the compiler is obviously dumber than you.
The rules say that to define an object of some type you need to provide
the type. Deduction of template arguments is only allowed by the
Standard in some contexts, and the constructor arguments for direct
initialization are not one of those contexts. A constructor is not a
regular function.
Find a copy of a good book that explains it and study. A decent book
enumerates the rules and shows what rules exist. A great book also
explains why such rules exist. The former is often enough, the latter
is sometimes too difficult to comprehend.
> [..]
V
--
I do not respond to top-posted replies, please don't ask