std::chrono:: treat_as_floating_point
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Non-member functions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Helper classes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Definido en el encabezado
<chrono>
|
||
|
template
<
class
Rep
>
struct treat_as_floating_point : std:: is_floating_point < Rep > { } ; |
(desde C++11) | |
El rasgo
std::chrono::treat_as_floating_point
ayuda a determinar si una duración puede convertirse a otra duración con un período de ticks diferente.
Las conversiones implícitas entre dos duraciones normalmente dependen del período de tic de las duraciones. Sin embargo, las conversiones implícitas pueden ocurrir independientemente del período de tic si std :: chrono :: treat_as_floating_point < Rep > :: value es true .
Contenidos |
Plantilla de variable auxiliar
|
template
<
class
Rep
>
constexpr bool treat_as_floating_point_v = treat_as_floating_point < Rep > :: value ; |
(desde C++17) | |
Especializaciones
std::chrono::treat_as_floating_point
puede especializarse para tipos definidos por el programa.
Ejemplo
#include <chrono> #include <iostream> #include <thread> void timed_piece_of_code() { std::chrono::milliseconds simulated_work(2); std::this_thread::sleep_for(simulated_work); } int main() { auto start = std::chrono::high_resolution_clock::now(); std::cout << "Running some timed piece of code...\n"; timed_piece_of_code(); auto stop = std::chrono::high_resolution_clock::now(); // A floating point milliseconds type using FpMilliseconds = std::chrono::duration<float, std::chrono::milliseconds::period>; static_assert(std::chrono::treat_as_floating_point<FpMilliseconds::rep>::value, "Rep required to be floating point"); // Note that implicit conversion is not allowed here auto i_ms = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start); // Note that implicit conversion is allowed here auto f_ms = FpMilliseconds(stop - start); std::cout << "Timing stats:\n"; std::cout << " Time in milliseconds, using default rep: " << i_ms.count() << '\n'; std::cout << " Time in milliseconds, using floating point rep: " << f_ms.count() << '\n'; }
Salida posible:
Running some timed piece of code... Timing stats: Time in milliseconds, using default rep: 2 Time in milliseconds, using floating point rep: 2.57307