22

In MSVC chrono implementation I see the following code

    _EXPORT_STD template <int = 0>
_NODISCARD constexpr year_month operator+(const year_month& _Left, const months& _Right) noexcept {
    const auto _Mo  = static_cast<long long>(static_cast<unsigned int>(_Left.month())) + (_Right.count() - 1);
    const auto _Div = (_Mo >= 0 ? _Mo : _Mo - 11) / 12;
    return year_month{_Left.year() + years{_Div}, month{static_cast<unsigned int>(_Mo - _Div * 12 + 1)}};
}

Can someone explain me why it uses template with unnamed parameter ?

4
  • 1
    Related post that possibly answers your question: stackoverflow.com/questions/59824884/…
    – CompuChip
    Sep 26 at 7:23
  • I understand that there are cases where unnamed template parameter can be useful, but I don't understand in this particular case. Why would we need specialization ? Especially with operator+ you can't even pass a template parameter when writing "ym + m" Sep 26 at 7:46
  • 3
    Candidate for C++ question (and answer) of the week award?
    – Bathsheba
    Sep 26 at 8:38
  • I still would try to avoid writing auto x = operator+<0>(ym, m);
    – Eljay
    Sep 26 at 13:21

1 Answer 1

29

You have two very similar overloads for operator+, originally without the template

template <int = 0>
constexpr year_month operator+(const year_month& _Left, const months& _Right) noexcept

constexpr year_month operator+(const year_month& _Left, const years& _Right) noexcept

It was discovered that if you have a value that is convertible to both months and years, there was an ambiguity here. Which conversion should be chosen?

By making one of the operators (a dummy) template, the non-template is chosen (if possible) because templates have lower priority in overload resolution.

The standard specifies this requirement a bit convoluted (backwards in my opinion):

"If the argument supplied by the caller for the months parameter is convertible to years, its implicit conversion sequence to years is worse than its implicit conversion sequence to months"

So if the conversions are equally good, the non-template is to be chosen. Only if conversion to months is better, the template gets a chance.

(And the standard doesn't explicitly say that is has to be a template, but that is a way of implementing this requirement).

3
  • 2
    And the reason for the specification to be this way is because adding a multiple of years can be implemented slightly more efficiently than adding months: Just add to the internal year field without touching the month field. Sep 26 at 13:22
  • 1
    @HowardHinnant what happens if the month is February and the day is 29? Surely you can't just do the math on year alone. Sep 27 at 20:17
  • 1
    This sounds like a good SO question because I'm not sure I can fit a proper answer into a comment. But briefly, the topic of this question is year_month. There is no day field in this type. You might like to ask that question of another type: year_month_day. And the briefest answer is: yes, you can just add to the year field. The result may or may not be a valid date. There's a way to check for that, and at least 5 different strategies that I can think of for dealing with it. Sep 27 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.