Frage

Ich habe einen Code, der eine Aufzählung mit einer Ganzzahl multipliziert:

QuantLib::Date date2 = date + 12 * QuantLib::Months;

Wobei QuantLib::Months definiert ist als:

enum TimeUnit { Days,
                Weeks,
                Months,
                Years
};

Dies gibt mir das gewünschte Ergebnis Datum2 ein Jahr später Datum.Allerdings kann ich nicht nachvollziehen, wie das zustande kommt.

Ich hatte gedacht, dass dies nicht kompiliert werden würde.Jetzt habe ich das Gefühl, dass ich zu einem „zwölf Monate“-Objekt komme, das dann von der QuantLib::Date „+“-Operatorüberladung verarbeitet wird, aber diesen Stil habe ich noch nie zuvor gesehen.

Da ich einen C#-Hintergrund habe, kann es sein, dass es hier etwas gibt, von dem ich nichts weiß.Kann jemand erklären, was los ist?Jede Referenzdokumentation wäre willkommen.

War es hilfreich?

Lösung

Hier gilt einer der folgenden Punkte:

  1. In C++ kann ein Aufzählungstyp implizit in einen Integraltyp konvertiert werden.Wenn das hier passiert, date + 12 * QuantLib::Months wäre das gleiche wie date + 12 * 2.

  2. Es ist auch möglich, Operatoren für Aufzählungstypen zu überladen.In diesem Fall könnte es sein, dass die Bibliothek eine definiert operator* (int, QuantLib::TimeUnit) was etwas zurückgibt, das mit dem kompatibel ist + Sie gehen.

Ich weiß nicht QuantLib, aber ich würde vermuten, dass Nummer 2 das ist, was passiert. QuantLib-Dokumentation bestätigt dies (danke an @DaliborFrivaldsky für den Link).

Andere Tipps

By default, all enumerations are basically integer constants, and as all integer values you can use them in arithmetic expressions.

In your case the constant QuantLib::Months has the value 2, as enumerations starts from zero and are simply increased.


However, unless you have to make your own data/time functionality, I suggest you use the functionality available in the standard library <chrono> header (or Boost chrono if you don't have a C++11 capable compiler/library). It has all this functionality built-in.

Here is an example similar to your code

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

The variable then will now be a time_point 24 * 365 hours in the future from now.

In your example you are using a so-called unscoped enumeration. Each enumeration has an underlying integral type (that is not necessary the type int There is no such a default rule for enumerations in C++). According to paragraph 4.5.3 of the C++ Standard (you asked some reference to documentation):

3 A prvalue of an unscoped enumeration type whose underlying type is not fixed (7.2) can be converted to a prvalue of the first of the following types that can represent all the values of the enumeration (i.e., the values in the range bmin to bmax as described in 7.2): int, unsigned int, long int, unsigned long int, long long int, or unsigned long long int. If none of the types in that list can represent all the values of the enumeration, a prvalue of an unscoped enumeration type can be converted to a prvalue of the extended integer type with lowest integer conversion rank (4.13) greater than the rank of long long in which all the values of the enumeration can be represented. If there are two such extended types, the signed one is chosen.

So in your example QuantLib::Months is converted to int because all values of the enumeration can be stored in an object of type int. Then usual arithmetic conversions are performed in the multiplicative operation.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top