boost-react-native-bundle
Version:
Boost library as in https://sourceforge.net/projects/boost/files/boost/1.57.0/
582 lines (578 loc) • 370 kB
HTML
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>User's Guide</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../chrono.html" title="Chapter 6. Boost.Chrono 2.0.4">
<link rel="prev" href="../chrono.html" title="Chapter 6. Boost.Chrono 2.0.4">
<link rel="next" href="reference.html" title="Reference">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../chrono.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../chrono.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="reference.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="chrono.users_guide"></a><a class="link" href="users_guide.html" title="User's Guide">User's Guide</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="users_guide.html#chrono.users_guide.getting_started">Getting Started</a></span></dt>
<dt><span class="section"><a href="users_guide.html#chrono.users_guide.tutorial">Tutorial</a></span></dt>
<dt><span class="section"><a href="users_guide.html#chrono.users_guide.examples">Examples</a></span></dt>
<dt><span class="section"><a href="users_guide.html#chrono.users_guide.ext_references">External Resources</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="chrono.users_guide.getting_started"></a><a class="link" href="users_guide.html#chrono.users_guide.getting_started" title="Getting Started">Getting Started</a>
</h3></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="users_guide.html#chrono.users_guide.getting_started.install">Installing
Chrono</a></span></dt>
<dt><span class="section"><a href="users_guide.html#chrono.users_guide.getting_started.hello_world__">Hello
World! </a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="chrono.users_guide.getting_started.install"></a><a class="link" href="users_guide.html#chrono.users_guide.getting_started.install" title="Installing Chrono">Installing
Chrono</a>
</h4></div></div></div>
<h6>
<a name="chrono.users_guide.getting_started.install.h0"></a>
<span class="phrase"><a name="chrono.users_guide.getting_started.install.getting__emphasis_role__bold__boost_chrono__emphasis__"></a></span><a class="link" href="users_guide.html#chrono.users_guide.getting_started.install.getting__emphasis_role__bold__boost_chrono__emphasis__">Getting
<span class="bold"><strong>Boost.Chrono</strong></span> </a>
</h6>
<p>
<span class="bold"><strong>Boost.Chrono</strong></span> is in the latest Boost release
in the folder <code class="computeroutput"><span class="special">/</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">chrono</span></code>.
Documentation, tests and examples folder are at <code class="computeroutput"><span class="identifier">boost</span><span class="special">/</span><span class="identifier">libs</span><span class="special">/</span><span class="identifier">chrono</span><span class="special">/</span></code>.
</p>
<p>
You can also access the latest (unstable?) state from the <a href="https://svn.boost.org/svn/boost-trunk" target="_top">Boost
trunk</a> directories boost/chrono and libs/chrono. Just go to <a href="http://svn.boost.org/trac/boost/wiki/BoostSubversion" target="_top">here</a>
and follow the instructions there for anonymous SVN access.
</p>
<h6>
<a name="chrono.users_guide.getting_started.install.h1"></a>
<span class="phrase"><a name="chrono.users_guide.getting_started.install.where_to_install_boost_chrono__"></a></span><a class="link" href="users_guide.html#chrono.users_guide.getting_started.install.where_to_install_boost_chrono__">Where
to install Boost.Chrono? </a>
</h6>
<p>
The simple way is to decompress (or checkout from SVN) the files in your
BOOST_ROOT directory.
</p>
<h6>
<a name="chrono.users_guide.getting_started.install.h2"></a>
<span class="phrase"><a name="chrono.users_guide.getting_started.install.building_boost_chrono_"></a></span><a class="link" href="users_guide.html#chrono.users_guide.getting_started.install.building_boost_chrono_">Building
Boost.Chrono </a>
</h6>
<p>
<span class="bold"><strong>Boost.Chrono</strong></span> can be configured as a header-only
library defining <a class="link" href="reference.html#chrono.reference.cpp0x.chrono_chrono_hpp.conf.header_only" title="How to Build Boost.Chrono as a Header Only Library?"><code class="computeroutput"><span class="identifier">BOOST_CHRONO_HEADER_ONLY</span></code></a>. However
Boost.Chrono depends on the non header-only library Boost.System, so that
you will need to link with boost_system.
</p>
<p>
Boost.System has un undocumented feature (use of macro BOOST_ERROR_CODE_HEADER_ONLY)
to make it header only.
</p>
<p>
If <a class="link" href="reference.html#chrono.reference.cpp0x.chrono_chrono_hpp.conf.header_only" title="How to Build Boost.Chrono as a Header Only Library?"><code class="computeroutput"><span class="identifier">BOOST_CHRONO_HEADER_ONLY</span></code></a> is not
defined you need to compile it and build the library before use, for example
using:
</p>
<pre class="programlisting"><span class="identifier">bjam</span> <span class="identifier">libs</span><span class="special">/</span><span class="identifier">chrono</span><span class="special">/</span><span class="identifier">build</span>
</pre>
<h6>
<a name="chrono.users_guide.getting_started.install.h3"></a>
<span class="phrase"><a name="chrono.users_guide.getting_started.install.requirements"></a></span><a class="link" href="users_guide.html#chrono.users_guide.getting_started.install.requirements">Requirements</a>
</h6>
<p>
In particular, <span class="bold"><strong>Boost.Chrono</strong></span> depends on:
</p>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term"><a href="http://www.boost.org/libs/config" target="_top"><span class="bold"><strong>Boost.Config</strong></span></a></span></dt>
<dd><p>
for configuration purposes, ...
</p></dd>
<dt><span class="term"><a href="http://www.boost.org/libs/exception" target="_top"><span class="bold"><strong>Boost.Exception</strong></span></a></span></dt>
<dd><p>
for throw_exception, ...
</p></dd>
<dt><span class="term"><a href="http://www.boost.org/libs/integer" target="_top"><span class="bold"><strong>Boost.Integer</strong></span></a></span></dt>
<dd><p>
for cstdint conformance, ...
</p></dd>
<dt><span class="term"><a href="http://www.boost.org/libs/mpl" target="_top"><span class="bold"><strong>Boost.MPL</strong></span></a></span></dt>
<dd><p>
for MPL Assert and bool, logical ...
</p></dd>
<dt><span class="term"><a href="http://www.boost.org/libs/operators" target="_top"><span class="bold"><strong>Boost.Operators</strong></span></a></span></dt>
<dd><p>
for operators, ...
</p></dd>
<dt><span class="term"><a href="http://www.boost.org/libs/ratio" target="_top"><span class="bold"><strong>Boost.Ratio</strong></span></a></span></dt>
<dd><p>
for ratio, milli, micro, ...
</p></dd>
<dt><span class="term"><a href="http://www.boost.org/libs/system" target="_top"><span class="bold"><strong>Boost.System</strong></span></a></span></dt>
<dd><p>
for error_code, ...
</p></dd>
<dt><span class="term"><a href="http://www.boost.org/libs/type_traits" target="_top"><span class="bold"><strong>Boost.TypeTraits</strong></span></a></span></dt>
<dd><p>
for is_base, is_convertible, common_type, ...
</p></dd>
<dt><span class="term"><a href="http://www.boost.org/libs/utility" target="_top"><span class="bold"><strong>Boost.Utility/EnableIf</strong></span></a></span></dt>
<dd><p>
for enable_if, ...
</p></dd>
</dl>
</div>
<h6>
<a name="chrono.users_guide.getting_started.install.h4"></a>
<span class="phrase"><a name="chrono.users_guide.getting_started.install.building_an_executable_that_uses_boost_chrono_"></a></span><a class="link" href="users_guide.html#chrono.users_guide.getting_started.install.building_an_executable_that_uses_boost_chrono_">Building
an Executable that Uses Boost.Chrono </a>
</h6>
<p>
In addition to link with the <span class="bold"><strong>Boost.Chrono</strong></span>
library you need also to link with the <span class="bold"><strong>Boost.System</strong></span>
library. If <span class="bold"><strong>Boost.System</strong></span> is configured
defining BOOST_ERROR_CODE_HEADER_ONLY you will no need to link with it
as the dependent part is header only then.
</p>
<h6>
<a name="chrono.users_guide.getting_started.install.h5"></a>
<span class="phrase"><a name="chrono.users_guide.getting_started.install.exception_safety_"></a></span><a class="link" href="users_guide.html#chrono.users_guide.getting_started.install.exception_safety_">Exception
safety </a>
</h6>
<p>
All functions in the library are exception-neutral and provide strong guarantee
of exception safety as long as the underlying parameters provide it.
</p>
<h6>
<a name="chrono.users_guide.getting_started.install.h6"></a>
<span class="phrase"><a name="chrono.users_guide.getting_started.install.thread_safety_"></a></span><a class="link" href="users_guide.html#chrono.users_guide.getting_started.install.thread_safety_">Thread
safety </a>
</h6>
<p>
All functions in the library are thread-unsafe except when noted explicitly.
</p>
<p>
As Boost.Chrono doesn't use mutable global variables the thread-safety
analysis is limited to the access to each instance variable. It is not
thread safe to use a function that modifies the access to a user variable
if another can be reading or writing it.
</p>
<h6>
<a name="chrono.users_guide.getting_started.install.h7"></a>
<span class="phrase"><a name="chrono.users_guide.getting_started.install.tested_compilers_"></a></span><a class="link" href="users_guide.html#chrono.users_guide.getting_started.install.tested_compilers_">Tested
compilers </a>
</h6>
<p>
The implementation will eventually work with most C++03 conforming compilers.
Currently I use to test with on:
</p>
<p>
Windows with
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
MSVC 10.0
</li></ul></div>
<p>
MinGW with
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
GCC 4.5.0
</li>
<li class="listitem">
GCC 4.5.0 -std=c++0x
</li>
<li class="listitem">
GCC 4.5.2
</li>
<li class="listitem">
GCC 4.5.2 -std=c++0x
</li>
<li class="listitem">
GCC 4.6.0
</li>
<li class="listitem">
GCC 4.6.0 -std=c++0x
</li>
<li class="listitem">
GCC 4.8.0
</li>
<li class="listitem">
GCC 4.8.0 -std=c++0x
</li>
</ul></div>
<p>
Ubuntu with * GCC 4.4.6 * GCC 4.4.6 -std=c++0x * GCC 4.5.4 * GCC 4.5.4
-std=c++0x * GCC 4.6.1 * GCC 4.6.1 -std=c++0x * Intel 12.1.3 * Intel 12.1.3
-std=c++0x
</p>
<p>
OsX with
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
GCC 4.1.2
</li>
<li class="listitem">
GCC 4.6.2
</li>
<li class="listitem">
GCC 4.6.2 -std=c++0x
</li>
<li class="listitem">
GCC 4.7.0
</li>
<li class="listitem">
GCC 4.7.0 -std=c++0x
</li>
<li class="listitem">
GCC 4.7.1
</li>
<li class="listitem">
GCC 4.7.1 -std=c++0x
</li>
<li class="listitem">
GCC 4.7.2
</li>
<li class="listitem">
GCC 4.7.2 -std=c++0x
</li>
<li class="listitem">
GCC 4.8.0
</li>
<li class="listitem">
GCC 4.8.0 -std=c++0x
</li>
<li class="listitem">
GCC 4.8.1
</li>
<li class="listitem">
GCC 4.8.1 -std=c++0x
</li>
<li class="listitem">
clang 3.1
</li>
<li class="listitem">
clang 3.1 -std=c++0x -stdlib=libc++
</li>
<li class="listitem">
clang 3.2
</li>
<li class="listitem">
clang 3.2 -std=c++11 -stdlib=libc++
</li>
</ul></div>
<p>
The committed code is tested with much more compilers. There are two compilers
(VACPP and Borland) that don't provide the needed features. Other as Intel
and Sun have some issues with i/o. While everything compiles and link correctly,
there are some runtime issues I have not cached yet. See the regression
tests for details.
</p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
Please let us know how this works on other platforms/compilers.
</p></td></tr>
</table></div>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
Please send any questions, comments and bug reports to boost <at>
lists <dot> boost <dot> org.
</p></td></tr>
</table></div>
</div>
<div class="section"><div class="titlepage"><div><div><h4 class="title">
<a name="chrono.users_guide.getting_started.hello_world__"></a><a class="link" href="users_guide.html#chrono.users_guide.getting_started.hello_world__" title="Hello World!">Hello
World! </a>
</h4></div></div></div></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="chrono.users_guide.tutorial"></a><a class="link" href="users_guide.html#chrono.users_guide.tutorial" title="Tutorial">Tutorial</a>
</h3></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="users_guide.html#chrono.users_guide.tutorial.duration">Duration</a></span></dt>
<dt><span class="section"><a href="users_guide.html#chrono.users_guide.tutorial.clocks">Clocks</a></span></dt>
<dt><span class="section"><a href="users_guide.html#chrono.users_guide.tutorial.time_point">Time Point</a></span></dt>
<dt><span class="section"><a href="users_guide.html#chrono.users_guide.tutorial.specific_clocks">Specific
Clocks</a></span></dt>
<dt><span class="section"><a href="users_guide.html#chrono.users_guide.tutorial.i_o">I/O</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="chrono.users_guide.tutorial.duration"></a><a class="link" href="users_guide.html#chrono.users_guide.tutorial.duration" title="Duration">Duration</a>
</h4></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="users_guide.html#chrono.users_guide.tutorial.duration.so_what_exactly_is_a__duration__and_how_do_i_use_one_">So
What Exactly is a <code class="computeroutput"><span class="identifier">duration</span></code>
and How Do I Use One?</a></span></dt>
<dt><span class="section"><a href="users_guide.html#chrono.users_guide.tutorial.duration.what_happens_if_i_assign__m3___us3__to__minutes__instead_of__microseconds__">What
Happens if I Assign <code class="computeroutput"><span class="identifier">m3</span> <span class="special">+</span> <span class="identifier">us3</span></code>
to <code class="computeroutput"><span class="identifier">minutes</span></code> Instead of
<code class="computeroutput"><span class="identifier">microseconds</span></code>?</a></span></dt>
<dt><span class="section"><a href="users_guide.html#chrono.users_guide.tutorial.duration.but_what_if_the_truncation_behavior_is_what_i_want_to_do_">But
What if the Truncation Behavior is What I Want to Do?</a></span></dt>
<dt><span class="section"><a href="users_guide.html#chrono.users_guide.tutorial.duration.round">Rounding
functions</a></span></dt>
<dt><span class="section"><a href="users_guide.html#chrono.users_guide.tutorial.duration.trafficking_in_floating_point_durations">Trafficking
in floating-point Durations</a></span></dt>
<dt><span class="section"><a href="users_guide.html#chrono.users_guide.tutorial.duration.how_expensive_is_all_of_this_">How
Expensive is All of this?</a></span></dt>
<dt><span class="section"><a href="users_guide.html#chrono.users_guide.tutorial.duration.how_complicated_is_it_to_build_a_function_taking_a__duration__parameter_">How
Complicated is it to Build a Function Taking a <code class="computeroutput"><span class="identifier">duration</span></code>
Parameter?</a></span></dt>
<dt><span class="section"><a href="users_guide.html#chrono.users_guide.tutorial.duration.is_it_possible_for_the_user_to_pass_a___duration_to_a_function_with_the_units_being_ambiguous_">Is
it possible for the user to pass a <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration" title="Class Template duration<>"><code class="computeroutput"><span class="identifier">duration</span></code></a> to a function with the
units being ambiguous?</a></span></dt>
<dt><span class="section"><a href="users_guide.html#chrono.users_guide.tutorial.duration.can_durations_overflow_">Can
Durations Overflow?</a></span></dt>
</dl></div>
<p>
The <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration" title="Class Template duration<>"><code class="computeroutput"><span class="identifier">duration</span></code></a> is the heart of this
library. The interface that the user will see in everyday use is nearly
identical to that of <span class="bold"><strong>Boost.DateTime</strong></span> time
<a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration" title="Class Template duration<>"><code class="computeroutput"><span class="identifier">duration</span></code></a>s authored by Jeff Garland,
both in syntax and in behavior. This has been a very popular boost library
for 7 years. There is an enormous positive history with this interface.
</p>
<p>
The library consists of six units of time <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration" title="Class Template duration<>"><code class="computeroutput"><span class="identifier">duration</span></code></a>:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
<a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_typedefs" title="duration typedefs"><code class="computeroutput"><span class="identifier">hours</span></code></a>
</li>
<li class="listitem">
<a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_typedefs" title="duration typedefs"><code class="computeroutput"><span class="identifier">minutes</span></code></a>
</li>
<li class="listitem">
<a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_typedefs" title="duration typedefs"><code class="computeroutput"><span class="identifier">seconds</span></code></a>
</li>
<li class="listitem">
<a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_typedefs" title="duration typedefs"><code class="computeroutput"><span class="identifier">milliseconds</span></code></a>
</li>
<li class="listitem">
<a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_typedefs" title="duration typedefs"><code class="computeroutput"><span class="identifier">microseconds</span></code></a>
</li>
<li class="listitem">
<a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_typedefs" title="duration typedefs"><code class="computeroutput"><span class="identifier">nanoseconds</span></code></a>
</li>
</ul></div>
<p>
These units were chosen as a subset of the boost library because they are
the most common units used when sleeping, waiting on a condition variable,
or waiting to obtain the lock on a mutex. Each of these units is nothing
but a thin wrapper around a signed integral count. That is, when you construct
<a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_typedefs" title="duration typedefs"><code class="computeroutput"><span class="identifier">minutes</span></code></a><code class="computeroutput"><span class="special">(</span><span class="number">3</span><span class="special">)</span></code>, all that
happens is a <code class="computeroutput"><span class="number">3</span></code> is stored inside
<code class="computeroutput"><span class="identifier">minutes</span></code>. When you construct
<a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_typedefs" title="duration typedefs"><code class="computeroutput"><span class="identifier">microseconds</span></code></a><code class="computeroutput"><span class="special">(</span><span class="number">3</span><span class="special">)</span></code>, all that
happens is a <code class="computeroutput"><span class="number">3</span></code> is stored inside
<a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_typedefs" title="duration typedefs"><code class="computeroutput"><span class="identifier">microseconds</span></code></a>.
</p>
<p>
The only context in which these different types differ is when being converted
to one another. At this time, unit-specific compile-time conversion constants
are used to convert the source unit to the target unit. Only conversions
from coarser units to finer units are allowed (in Boost). This restriction
ensures that all conversions are always exact. That is, <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_typedefs" title="duration typedefs"><code class="computeroutput"><span class="identifier">microseconds</span></code></a> can always represent
any value <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_typedefs" title="duration typedefs"><code class="computeroutput"><span class="identifier">minutes</span></code></a> has.
</p>
<p>
In <span class="bold"><strong>Boost.DateTime</strong></span>, these units are united
via inheritance. <span class="bold"><strong>Boost.Chrono</strong></span> instead
unites these units through the class template <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration" title="Class Template duration<>"><code class="computeroutput"><span class="identifier">duration</span></code></a>. That is, in <span class="bold"><strong>Boost.Chrono</strong></span> all six of the above units are nothing
but typedefs to different instantiations of <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration" title="Class Template duration<>"><code class="computeroutput"><span class="identifier">duration</span></code></a>. This change from Boost.DateTime
has a far reaching positive impact, while not changing the syntax of the
everyday use at all.
</p>
<p>
The most immediate positive impact is that the library can immediately
generate any unit, with any precision it needs. This is sometimes necessary
when doing comparisons or arithmetic between <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration" title="Class Template duration<>"><code class="computeroutput"><span class="identifier">duration</span></code></a>s of differing precision,
assuming one wants the comparison and arithmetic to be exact.
</p>
<p>
A secondary benefit is that by publishing the class template <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration" title="Class Template duration<>"><code class="computeroutput"><span class="identifier">duration</span></code></a> interface, user code
can very easily create <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration" title="Class Template duration<>"><code class="computeroutput"><span class="identifier">duration</span></code></a>s with any precision they
desire. The <code class="computeroutput"><span class="identifier">ratio</span></code> utility
is used to specify the precision, so as long as the precision can be expressed
by a rational constant with respect to seconds, this framework can exactly
represent it (one third of a second is no problem, and neither is one third
of a <code class="computeroutput"><span class="identifier">femto</span></code> second). All
of this utility and flexibility comes at no cost just by making use of
the no-run-time-overhead <code class="computeroutput"><span class="identifier">ratio</span></code>
facility.
</p>
<p>
In Boost.DateTime, <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_typedefs" title="duration typedefs"><code class="computeroutput"><span class="identifier">hours</span></code></a> does not have the same representation
as <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_typedefs" title="duration typedefs"><code class="computeroutput"><span class="identifier">nanoseconds</span></code></a>. The former is usually
represented with a <code class="computeroutput"><span class="keyword">long</span></code> whereas
a <code class="computeroutput"><span class="keyword">long</span> <span class="keyword">long</span></code>
is required for the latter. The reason for this is simply range. You don't
need many hours to cover an extremely large range of time. But this isn't
true of nanoseconds. Being able to reduce the sizeof overhead for some
units when possible, can be a significant performance advantage.
</p>
<p>
<span class="bold"><strong>Boost.Chrono</strong></span> continues, and generalizes
that philosophy. Not only can one specify the precision of a <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration" title="Class Template duration<>"><code class="computeroutput"><span class="identifier">duration</span></code></a>, one can also specify
its representation. This can be any integral type, or even a floating-point
type. Or it can be a user-defined type which emulates an arithmetic type.
The six predefined units all use signed integral types as their representation.
And they all have a minimum range of ± 292 years. <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_typedefs" title="duration typedefs"><code class="computeroutput"><span class="identifier">nanoseconds</span></code></a> needs 64 bits to cover
that range. <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_typedefs" title="duration typedefs"><code class="computeroutput"><span class="identifier">hours</span></code></a> needs only 23 bits to cover
that range.
</p>
<div class="section">
<div class="titlepage"><div><div><h5 class="title">
<a name="chrono.users_guide.tutorial.duration.so_what_exactly_is_a__duration__and_how_do_i_use_one_"></a><a class="link" href="users_guide.html#chrono.users_guide.tutorial.duration.so_what_exactly_is_a__duration__and_how_do_i_use_one_" title="So What Exactly is a duration and How Do I Use One?">So
What Exactly is a <code class="computeroutput"><span class="identifier">duration</span></code>
and How Do I Use One?</a>
</h5></div></div></div>
<p>
A <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration" title="Class Template duration<>"><code class="computeroutput"><span class="identifier">duration</span></code></a> has a representation
and a tick period (precision).
</p>
<pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Period</span> <span class="special">=</span> <code class="computeroutput"><span class="identifier">ratio</span></code><span class="special"><</span><span class="number">1</span><span class="special">></span> <span class="special">></span> <span class="keyword">class</span> <span class="identifier">duration</span><span class="special">;</span>
</pre>
<p>
The representation is simply any arithmetic type, or an emulation of
such a type. The representation stores a count of ticks. This count is
the only data member stored in a <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration" title="Class Template duration<>"><code class="computeroutput"><span class="identifier">duration</span></code></a>. If the representation
is floating-point, it can store fractions of a tick to the precision
of the representation. The tick period is represented by a <code class="computeroutput"><span class="identifier">ratio</span></code> and is encoded into the <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration" title="Class Template duration<>"><code class="computeroutput"><span class="identifier">duration</span></code></a>'s type, instead of
stored. The tick period only has an impact on the behavior of the <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration" title="Class Template duration<>"><code class="computeroutput"><span class="identifier">duration</span></code></a> when a conversion between
different <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration" title="Class Template duration<>"><code class="computeroutput"><span class="identifier">duration</span></code></a>s is attempted. The
tick period is completely ignored when simply doing arithmetic among
like <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration" title="Class Template duration<>"><code class="computeroutput"><span class="identifier">duration</span></code></a>s.
</p>
<p>
<span class="bold"><strong>Example:</strong></span>
</p>
<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration" title="Class Template duration<>"><code class="computeroutput"><span class="identifier">duration</span></code></a><span class="special"><</span><span class="keyword">long</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">ratio</span><span class="special"><</span><span class="number">60</span><span class="special">></span> <span class="special">></span> <span class="identifier">minutes</span><span class="special">;</span>
<span class="identifier">minutes</span> <span class="identifier">m1</span><span class="special">(</span><span class="number">3</span><span class="special">);</span> <span class="comment">// m1 stores 3</span>
<span class="identifier">minutes</span> <span class="identifier">m2</span><span class="special">(</span><span class="number">2</span><span class="special">);</span> <span class="comment">// m2 stores 2</span>
<span class="identifier">minutes</span> <span class="identifier">m3</span> <span class="special">=</span> <span class="identifier">m1</span> <span class="special">+</span> <span class="identifier">m2</span><span class="special">;</span> <span class="comment">// m3 stores 5</span>
<span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration" title="Class Template duration<>"><code class="computeroutput"><span class="identifier">duration</span></code></a><span class="special"><</span><span class="keyword">long</span> <span class="keyword">long</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">micro</span><span class="special">></span> <span class="identifier">microseconds</span><span class="special">;</span>
<span class="identifier">microseconds</span> <span class="identifier">us1</span><span class="special">(</span><span class="number">3</span><span class="special">);</span> <span class="comment">// us1 stores 3</span>
<span class="identifier">microseconds</span> <span class="identifier">us2</span><span class="special">(</span><span class="number">2</span><span class="special">);</span> <span class="comment">// us2 stores 2</span>
<span class="identifier">microseconds</span> <span class="identifier">us3</span> <span class="special">=</span> <span class="identifier">us1</span> <span class="special">+</span> <span class="identifier">us2</span><span class="special">;</span> <span class="comment">// us3 stores 5</span>
<span class="identifier">microseconds</span> <span class="identifier">us4</span> <span class="special">=</span> <span class="identifier">m3</span> <span class="special">+</span> <span class="identifier">us3</span><span class="special">;</span> <span class="comment">// us4 stores 300000005</span>
</pre>
<p>
In the final line of code above, there is an implicit conversion from
minutes to microseconds, resulting in a relatively large number of microseconds.
</p>
<p>
If you need to access the tick count within a <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration" title="Class Template duration<>"><code class="computeroutput"><span class="identifier">duration</span></code></a>, there is a member
<code class="computeroutput"><span class="identifier">count</span><span class="special">()</span></code>
which simply returns the stored tick count.
</p>
<pre class="programlisting"><span class="keyword">long</span> <span class="keyword">long</span> <span class="identifier">tc</span> <span class="special">=</span> <span class="identifier">us4</span><span class="special">.</span><span class="identifier">count</span><span class="special">();</span> <span class="comment">// tc is 300000005</span>
</pre>
<p>
These <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration" title="Class Template duration<>"><code class="computeroutput"><span class="identifier">duration</span></code></a>'s have very simple,
very predictable, and very observable behavior. After all, this is really
nothing but the time-tested interface of Jeff's boost time <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration" title="Class Template duration<>"><code class="computeroutput"><span class="identifier">duration</span></code></a> library (unified with
templates instead of inheritance).
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h5 class="title">
<a name="chrono.users_guide.tutorial.duration.what_happens_if_i_assign__m3___us3__to__minutes__instead_of__microseconds__"></a><a class="link" href="users_guide.html#chrono.users_guide.tutorial.duration.what_happens_if_i_assign__m3___us3__to__minutes__instead_of__microseconds__" title="What Happens if I Assign m3 + us3 to minutes Instead of microseconds?">What
Happens if I Assign <code class="computeroutput"><span class="identifier">m3</span> <span class="special">+</span> <span class="identifier">us3</span></code>
to <code class="computeroutput"><span class="identifier">minutes</span></code> Instead of
<code class="computeroutput"><span class="identifier">microseconds</span></code>?</a>
</h5></div></div></div>
<pre class="programlisting"><span class="identifier">minutes</span> <span class="identifier">m4</span> <span class="special">=</span> <span class="identifier">m3</span> <span class="special">+</span> <span class="identifier">us3</span><span class="special">;</span>
</pre>
<p>
It won't compile! The rationale is that implicit truncation error should
not be allowed to happen. If this were to compile, then <code class="computeroutput"><span class="identifier">m4</span></code> would hold <code class="computeroutput"><span class="number">5</span></code>,
the same value as <code class="computeroutput"><span class="identifier">m3</span></code>.
The value associated with <code class="computeroutput"><span class="identifier">us3</span></code>
has been effectively ignored. This is similar to the problem of assigning
a double to an <code class="computeroutput"><span class="keyword">int</span></code>: the
fractional part gets silently discarded.
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h5 class="title">
<a name="chrono.users_guide.tutorial.duration.but_what_if_the_truncation_behavior_is_what_i_want_to_do_"></a><a class="link" href="users_guide.html#chrono.users_guide.tutorial.duration.but_what_if_the_truncation_behavior_is_what_i_want_to_do_" title="But What if the Truncation Behavior is What I Want to Do?">But
What if the Truncation Behavior is What I Want to Do?</a>
</h5></div></div></div>
<p>
There is a <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_cast" title="Non-Member Function duration_cast(duration)"><code class="computeroutput"><span class="identifier">duration_cast</span></code></a> facility to explicitly
ask for this behavior:
</p>
<pre class="programlisting"><span class="identifier">minutes</span> <span class="identifier">m4</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_cast" title="Non-Member Function duration_cast(duration)"><code class="computeroutput"><span class="identifier">duration_cast</span></code></a><span class="special"><</span><span class="identifier">minutes</span><span class="special">>(</span><span class="identifier">m3</span> <span class="special">+</span> <span class="identifier">us3</span><span class="special">);</span> <span class="comment">// m4.count() == 5</span>
</pre>
<p>
In general, one can perform <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration" title="Class Template duration<>"><code class="computeroutput"><span class="identifier">duration</span></code></a> arithmetic at will.
If <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_cast" title="Non-Member Function duration_cast(duration)"><code class="computeroutput"><span class="identifier">duration_cast</span></code></a> isn't used, and
it compiles, the arithmetic is exact. If one wants to override this exact
arithmetic behavior, <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_cast" title="Non-Member Function duration_cast(duration)"><code class="computeroutput"><span class="identifier">duration_cast</span></code></a> can be used to
explicitly specify that desire. The <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_cast" title="Non-Member Function duration_cast(duration)"><code class="computeroutput"><span class="identifier">duration_cast</span></code></a> has the same efficiency
as the implicit conversion, and will even be exact as often as it can.
</p>
<p>
You can use <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_cast" title="Non-Member Function duration_cast(duration)"><code class="computeroutput"><span class="identifier">duration_cast</span></code></a><code class="computeroutput"><span class="special"><></span></code>
to convert the <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration" title="Class Template duration<>"><code class="computeroutput"><span class="identifier">duration</span></code></a> into whatever units
you desire. This facility will round down (truncate) if an exact conversion
is not possible. For example:
</p>
<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_typedefs" title="duration typedefs"><code class="computeroutput"><span class="identifier">nanoseconds</span></code></a> <span class="identifier">start</span><span class="special">;</span>
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_typedefs" title="duration typedefs"><code class="computeroutput"><span class="identifier">nanoseconds</span></code></a> <span class="identifier">end</span><span class="special">;</span>
<span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_typedefs" title="duration typedefs"><code class="computeroutput"><span class="identifier">milliseconds</span></code></a> <span class="identifier">ms</span><span class="special">;</span>
<span class="identifier">ms</span> <span class="identifier">d</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_cast" title="Non-Member Function duration_cast(duration)"><code class="computeroutput"><span class="identifier">duration_cast</span></code></a><span class="special"><</span><span class="identifier">ms</span><span class="special">>(</span><span class="identifier">end</span> <span class="special">-</span> <span class="identifier">start</span><span class="special">);</span>
<span class="comment">// d now holds the number of milliseconds from start to end.</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">ms</span><span class="special">.</span><span class="identifier">count</span><span class="special">()</span> <span class="special"><<</span> <span class="string">"ms\n"</span><span class="special">;</span>
</pre>
<p>
We can convert to <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_typedefs" title="duration typedefs"><code class="computeroutput"><span class="identifier">nanoseconds</span></code></a>, or some integral-based
duration which <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_typedefs" title="duration typedefs"><code class="computeroutput"><span class="identifier">nanoseconds</span></code></a> will always exactly
convert to, then <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_cast" title="Non-Member Function duration_cast(duration)"><code class="computeroutput"><span class="identifier">duration_cast</span></code></a><code class="computeroutput"><span class="special"><></span></code>
is unnecessary:
</p>
<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_typedefs" title="duration typedefs"><code class="computeroutput"><span class="identifier">nanoseconds</span></code></a> <span class="identifier">ns</span><span class="special">;</span>
<span class="identifier">ns</span> <span class="identifier">d</span> <span class="special">=</span> <span class="identifier">end</span> <span class="special">-</span> <span class="identifier">start</span><span class="special">;</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">ns</span><span class="special">.</span><span class="identifier">count</span><span class="special">()</span> <span class="special"><<</span> <span class="string">"ns\n"</span><span class="special">;</span>
</pre>
<p>
If you need seconds with a floating-point representation you can also
eliminate the <a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration_cast" title="Non-Member Function duration_cast(duration)"><code class="computeroutput"><span class="identifier">duration_cast</span></code></a><code class="computeroutput"><span class="special"><></span></code>:
</p>
<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><a class="link" href="reference.html#chrono.reference.cpp0x.duration_hpp.duration" title="Class Template duration<>"><code class="computeroutput"><span class="identifier">duration</span></code></a><span class="special"><</span><span class="keyword">double</span><span class="special">></span> <span class="identifier">sec</span><span class="special">;</span> <span class="comment">// seconds, stored with a double</span>
<span class="identifier">sec</span> <span class="identifier">d</span> <span class="special">=</span> <span class="identifier">end</span> <span class="special">-</span> <span class="identifier">start</span><span class="special">;</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">sec</span><span class="special">.</span><span class="identifier">count</span><span class="special">()</span> <span class="special"><<</span> <span class="string">"s\n"</span><span class="special">;</span>
</pre>
<p>
If you're not sure if you need <a class="link" href="reference.h