I would like a setw to take two parameters and return the largest. Is this possible? How do I go about doing this? Not looking for code just some direction would be fine as I couldn't find a clear answer on-the-line.
I don't think you can overload it, but you can certainly define your own type with a different name and then define <<
and >>
operators for it. For example:
struct setw_largest
{
int _value;
setw_largest(int value1, int value2) : _value(std::max(value1, value2)) {}
};
template<class _Elem, class _Traits, class _Arg>
inline basic_istream<_Elem, _Traits>& operator>>(basic_istream<_Elem, _Traits>& _Istr, const setw_largest& _Manip)
{
_Istr.width(_Manip._value);
return _Istr;
}
template<class _Elem, class _Traits, class _Arg>
inline basic_ostream<_Elem, _Traits>& operator<<(basic_ostream<_Elem, _Traits>& _Ostr, const setw_largest& _Manip)
{
_Ostr.width(_Manip._value);
return _Ostr;
}
std::cin >> setw_largest(1, 2) >> ...;
std::cout << setw_largest(1, 2) << ...;