Multiplication Enum in C ++

I have code that multiplies 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 for date2, which is one year from the date. However, I cannot understand how this is achieved.

I thought this would not compile. Now I feel like I am flying to the "twelve months" object, which is then processed by overloading the QuantLib :: Date '+' operator, but I have never seen this before.

I came from C # background, so there might be something I don't know about here. Can anyone explain what is happening? Any background documentation would be appreciated.

+3
source share
3 answers

One of the following actions applies here:

  • ++ . , date + 12 * QuantLib::Months , date + 12 * 2.

  • . , operator* (int, QuantLib::TimeUnit), - +, .

QuantLib, , , . QuantLib ( @DaliborFrivaldsky ).

+4

, .

QuantLib::Months 2, .


, , , <chrono> header ( Boost chrono, ++ 11 /). .

,

auto now = std::chrono::system_clock::now();  // The current time at the moment
auto then = now + std::chrono::hours(24 * 365);

then time_point 24 * 365 now.

+1

. ( int ++). 4.5.3 ++ ( ):

3 , (7.2) prvalue , ( bmin bmax, 7.2): int, unsigned int, long int, unsigned long int, long long int unsigned long long int. , prvalue (4.13), . , .

So, in your example it is QuantLib::Monthsconverted to int, because all enumeration values ​​can be stored in an object of type int. Then, in the multiplicative operation, the usual arithmetic transformations are performed.

0
source

All Articles