HI,
I started learning C++ STL’s
i am just trying some small programs.one of them is below:
inline int const& max (int const& a, int const& b)
{
return a < b ? b : a;
}
template <typename T>
inline T const& max (T const& a, T const& b)
{
return a < b ? b : a;
}
int main()
{
::max(7, 42); // calls the nontemplate for two ints
::max<>(7, 42); // calls max<int> (by argument deduction)
::max('a', 42.7); // calls the nontemplate for two ints
}
I have some basic questions!!
-
why is the scope resolution
operator used here? -
why/how is
that calling ::max<>(7, 42) will
assume that the parameter passed areintegers?
Probably to differentiate the max declared here from the one in (for example) the std:: namespace.
It doesn’t have to assume anything – integer literals have the type int.
And to answer the question you didn’t ask:
matches the non-template version because type conversions are not performed on templated parameters, but are performed on non-template ones.