I have some code that is multiplying an enum by an integer:
QuantLib::Date date2 = date + 12 * QuantLib::Months;
Where QuantLib::Months is defined as:
enum TimeUnit { Days,
Weeks,
Months,
Years
};
This gives me the desired result of date2 being one year on from date. However, I'm not able to comprehend how this is being achieved.
I had thought that this would not compile. Now I feel that I'm arriving at a "twelve months" object, which the is then handled by the QuantLib::Date '+' operator overload, but I've never seen this style before.
I have come from a C# background, so there may be something I'm not aware of at work here. Can anyone explain what is going on? Any reference documentation would be appreciated.
One of the following is in effect here:
In C++, an enumeration type can be implicitly converted to an integral type. If this is happening here, date + 12 * QuantLib::Months would be the same as date + 12 * 2.
It is also possible to overload operators for enumeration types. In that case, it might be that the library defines an operator* (int, QuantLib::TimeUnit) which returns something compatible with the + you're doing.
I don't know QuantLib, but I'd guess #2 is what's happening. QuantLib documentation corroborates this (thanks to @DaliborFrivaldsky for the link).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With