In order to make usage fully concise (avoid repetition), we can use a template function and implement the idea proposed by 5gon12eder 5gon12eder, in this this comment which simply creates the time_sink
object and uses template function type deduction to fill in the details.
As 5gon12eder 5gon12eder points out, you must also ensure that you keep the return of make_time_sink
in order to prevent time_sink
's destructor from running early, since destructors are called at the "end of the full expression, for nameless temporaries" (source).
In order to make usage fully concise (avoid repetition), we can use a template function and implement the idea proposed by 5gon12eder, in this comment which simply creates the time_sink
object and uses template function type deduction to fill in the details.
As 5gon12eder points out, you must also ensure that you keep the return of make_time_sink
in order to prevent time_sink
's destructor from running early, since destructors are called at the "end of the full expression, for nameless temporaries" (source).
In order to make usage fully concise (avoid repetition), we can use a template function and implement the idea proposed by 5gon12eder, in this comment which simply creates the time_sink
object and uses template function type deduction to fill in the details.
As 5gon12eder points out, you must also ensure that you keep the return of make_time_sink
in order to prevent time_sink
's destructor from running early, since destructors are called at the "end of the full expression, for nameless temporaries" (source).
#include <chrono>
#include <thread>
template <typename TimeUnit, typename Clock = std::chrono::high_resolution_clock>
class time_sink
{
public:
using duration = TimeUnit;
time_sink( TimeUnit const minimum_duration ) :
min_duration{ minimum_duration },
begin{ Clock::now() }
{}
~time_sink()
{
auto time_spent = Clock::now() - begin;
if ( time_spent < min_duration )
{
std::this_thread::sleep_for( min_duration - time_spent );
}
}
private:
TimeUnit min_duration;
std::chrono::time_point<Clock> begin;
};
template <typename TimeUnit, typename Clock = std::chrono::high_resolution_clock>
time_sink<TimeUnit, Clock> make_time_sink( TimeUnit const minimum_duration )
{
return time_sink<TimeUnit, Clock>{ minimum_duration };
}
#include <chrono>
#include <thread>
template <typename TimeUnit, typename Clock = std::chrono::high_resolution_clock>
class time_sink
{
public:
using duration = TimeUnit;
time_sink( TimeUnit const minimum_duration ) :
min_duration{ minimum_duration },
begin{ Clock::now() }
{}
~time_sink()
{
auto time_spent = Clock::now() - begin;
if ( time_spent < min_duration )
{
std::this_thread::sleep_for( min_duration - time_spent );
}
}
private:
TimeUnit min_duration;
std::chrono::time_point<Clock> begin;
};
template <typename TimeUnit, typename Clock = std::chrono::high_resolution_clock>
time_sink<TimeUnit, Clock> make_time_sink( TimeUnit const minimum_duration )
{
return time_sink<TimeUnit, Clock>{ minimum_duration };
}
#include <chrono>
#include <thread>
template <typename TimeUnit, typename Clock = std::chrono::high_resolution_clock>
class time_sink
{
public:
time_sink( TimeUnit const minimum_duration ) :
min_duration{ minimum_duration },
begin{ Clock::now() }
{}
~time_sink()
{
auto time_spent = Clock::now() - begin;
if ( time_spent < min_duration )
{
std::this_thread::sleep_for( min_duration - time_spent );
}
}
private:
TimeUnit min_duration;
std::chrono::time_point<Clock> begin;
};
template <typename TimeUnit, typename Clock = std::chrono::high_resolution_clock>
time_sink<TimeUnit, Clock> make_time_sink( TimeUnit const minimum_duration )
{
return time_sink<TimeUnit, Clock>{ minimum_duration };
}