Domanda

Ho qualche codice che sta moltiplicando un enum da un numero intero:

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

Dove Quantib :: mesi è definito come:

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

Questo mi dà il risultato desiderato di data2 essere un anno da data .Tuttavia, non sono in grado di comprendere come questo è stato raggiunto.

Avevo pensato che questo non sarebbe stato compilato.Ora sento che sto arrivando ad un oggetto "dodici mesi", che viene quindi gestito dal Quantib :: date '+' sovraccarico dell'operatore, ma non ho mai visto questo stile prima.

Sono venuto da uno sfondo C #, quindi potrebbe esserci qualcosa che non sono a conoscenza del lavoro qui.Qualcuno può spiegare cosa sta succedendo?Qualsiasi documentazione di riferimento sarebbe apprezzata.

È stato utile?

Soluzione

Uno dei seguenti è in effetti qui:

    .
  1. In C ++, un tipo di enumerazione può essere implicitamente convertito in un tipo integrale.Se questo sta accadendo qui, date + 12 * QuantLib::Months sarebbe uguale a date + 12 * 2.

  2. È anche possibile sovraccaricare gli operatori per i tipi di enumerazione.In tal caso, potrebbe essere che la biblioteca definisca un operator* (int, QuantLib::TimeUnit) che restituisca qualcosa compatibile con il + che stai facendo.

  3. Non conosco QuantLib, ma indovrei il # 2 è ciò che sta succedendo. Documentazione QUANTILB corrobora questo (grazie a @DaliborFrivalDsky per il link).

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top