//We noted in §4.2.3/65 that it is essential that the argument types in a call to max
match exactly. Will the following code work? If there is a problem, how would you fix it?
int maxlen;
Student_info s;
max(s.name.size(), maxlen);
Hint
Consider the return type of s.name.size()
.
Solution
There is a problem with the code. The type returned from s.name.size()
is string::size_type
, which does not match the type of maxlen
. To solve the problem, change maxlen
‘s declaration to string::size_type
.
#include <algorithm>
#include <string>
#include <vector>
#ifdef _MSC_VER
#undef max
#endif
using std::max;
using std::string;
using std::vector;
struct Student_info {
string name;
double midterm, final;
vector<double> homework;
};
int main()
{
string::size_type maxlen;
Student_info s;
max(s.name.size(), maxlen);
}