Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

This This stack overflow question should tell you why that's the preferred clock type for timing function execution.

This stack overflow question should tell you why that's the preferred clock type for timing function execution.

This stack overflow question should tell you why that's the preferred clock type for timing function execution.

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

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).

deleted 42 characters in body
Source Link
user2296177
  • 3.6k
  • 1
  • 15
  • 37
#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 };
}
added 590 characters in body
Source Link
user2296177
  • 3.6k
  • 1
  • 15
  • 37
Loading
added 1052 characters in body
Source Link
user2296177
  • 3.6k
  • 1
  • 15
  • 37
Loading
added 860 characters in body
Source Link
user2296177
  • 3.6k
  • 1
  • 15
  • 37
Loading
added 74 characters in body
Source Link
user2296177
  • 3.6k
  • 1
  • 15
  • 37
Loading
Source Link
user2296177
  • 3.6k
  • 1
  • 15
  • 37
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /