Duration
Overview
Type safe representation of duration: the length of time that something lasts or continues, measured in seconds.
Declaration
struct duration_unit;
struct duration : _unspecified_base_type_
{
using base_type = _unspecified_base_type_;
using base_type::base_type;
using unit_type = duration_unit;
};
// Free functions
constexpr double as_double(duration d);
constexpr float as_float(duration d);
// Literals
inline namespace literals
{
constexpr duration operator "" _s(long double val);
constexpr duration operator "" _s(unsigned long long int val);
constexpr duration operator "" _ms(long double val);
constexpr duration operator "" _ms(unsigned long long int val);
constexpr duration operator "" _us(long double val);
constexpr duration operator "" _us(unsigned long long int val);
}
Expressions
duration is a model of Unit. In addition to valid expressions for Unit, duration allows these expressions.
Notation
d-
Object of type
duration. 1.0-
A floating point literal number.
1-
An integer literal number.
Conversions
| Expression | Semantics | Return Type |
|---|---|---|
|
Convert duration to a |
|
|
Convert duration to a |
|
Literals
If you need to create duration from numeric constants, it is preferable to use duration literals instead. Example:
#include <q/support/literals.hpp>
...
using namespace cycfi::q::literals;
auto d = 6_ms;
| Expression | Semantics |
|---|---|
|
Returns a |
|
Returns a |
|
Returns a |
|
Returns a |
|
Returns a |
|
Returns a |
Take note that 1.0 and 1 stand for any floating point, and integer numeric literals.
Unless you have a conflicting usage for the _s, _ms, and _us literals, it is generally safe to bring their namespace into scope using the using namespace cycfi::q::literals directive anywhere in a cpp file.
|
Example
Get the number of samples in a 50 ms attack, at sps samples per second:
namespace q = cycfi::q;
using namespace q::literals;
auto attack = 50_ms; (1)
auto samples = std::size_t(q::as_double(attack) * sps); (2)
| 1 | Construct a duration of 50 milliseconds. |
| 2 | Convert the duration to seconds, then to samples. |