Online Book Reader

Home Category

Professional C__ - Marc Gregoire [279]

By Root 1574 0
constants come in handy for defining tick periods. For example, the following line of code defines a duration where each tick period is one millisecond:

duration d4;

The following example demonstrates several aspects of durations. It shows you how to define durations, how to perform arithmetic operations on them, and how to convert one duration into another duration with a different tick period:

// Specify a duration where each tick is 60 seconds

duration> d1(123);

cout << d1.count() << endl;

// Specify a duration represented by a double with each tick

// equal to 1 second and assign the largest possible duration to it.

duration d2;

d2 = d2.max();

cout << d2.count() << endl;

// Define 2 durations:

// For the first duration, each tick is 1 minute

// For the second duration, each tick is 1 second

duration> d3(10); // = 10 minutes

duration> d4(14); // = 14 seconds

// Compare both durations

if (d3 > d4)

cout << "d3 > d4" << endl;

else

cout << "d3 <= d4" << endl;

// Increment d4 with 1 resulting in 15 seconds

++d4;

// Multiply d4 by 2 resulting in 30 seconds

d4 *= 2;

// Add both durations and store as minutes

duration> d5 = d3 + d4;

// Add both durations and store as seconds

duration> d6 = d3 + d4;

cout << d3.count() << " minutes + " << d4.count() << " seconds = "

<< d5.count() << " minutes or "

<< d6.count() << " seconds" << endl;

// Create a duration of 30 seconds

duration d7(30);

// Convert the seconds of d7 to minutes

duration> d8(d7);

cout << d7.count() << " seconds = " << d8.count() << " minutes" << endl;

Code snippet from Chrono\durations.cpp

The output is as follows:

123

1.79769e+308

d3 > d4

10 minutes + 30 seconds = 10.5 minutes or 630 seconds

30 seconds = 0.5 minutes

The second line in the output represents the largest possible duration with type double. The exact value might be different depending on your compiler.

Pay special attention to the following two lines:

duration> d5 = d3 + d4;

duration> d6 = d3 + d4;

They both calculate d3+d4 but the first one stores it as a floating point value representing minutes while the second one stores the result as an integer representing seconds. Conversion from minutes to seconds or vice versa happens automatically.

The following two lines from the preceding example demonstrate how to convert between different units of time:

duration d7(30); // seconds

duration> d8(d7); // minutes

The first line defines a duration representing 30 seconds. The second line converts these 30 seconds into minutes, resulting in 0.5 minutes. Converting in this direction can result in a non-integral value and thus requires you to use a duration represented by a floating point type; otherwise, you will get some cryptic compiler errors. The following lines, for example, will not compile because d8 is using long instead of a floating point type:

duration d7(30); // seconds

duration> d8(d7); // minutes // Error

Converting in the other direction does not require floating point types if the source is an integral type, because the result is always an integral value if you started with an integral value. For example, the following lines convert 10 minutes into seconds, both represented by the integral type long:

duration> d9(10); // minutes

duration d10(d9); // seconds

The library also provides the following standard duration types, where X stands for “signed integer type of at least”:

typedef duration nanoseconds;

typedef duration microseconds;

typedef duration milliseconds;

typedef duration seconds;

typedef duration> minutes;

typedef duration> hours;

The exact type of X depends on your compiler, but the C++ standard requires it to be a signed integer type of at least the specified size. The preceding typedefs

Return Main Page Previous Page Next Page

®Online Book Reader