duration_operator_arith4

duration_operator_arith4 (기간 산술 연산)

std::chrono::duration에 대한 operator+, -, *, /, % 연산자예요. 두 기간 사이 또는 기간과 틱 수 사이의 기본 산술을 수행해요.

출처: cppreference

본문

std::chrono::duration의 산술 연산자는 <chrono> 헤더에 정의되어 있어요.

template< class Rep1, class Period1, class Rep2, class Period2 >
constexpr std::common_type_t<duration<Rep1, Period1>, duration<Rep2, Period2>>
    operator+( const duration<Rep1, Period1>& lhs,
               const duration<Rep2, Period2>& rhs );

template< class Rep1, class Period1, class Rep2, class Period2 >
constexpr std::common_type_t<duration<Rep1, Period1>, duration<Rep2, Period2>>
    operator-( const duration<Rep1, Period1>& lhs,
               const duration<Rep2, Period2>& rhs );
  • operator+ — 두 기간을 공통 타입으로 변환하고, 변환 후 틱 수의 합으로 이루어진 기간을 만들어요.
  • operator- — 두 기간의 차.
  • operator* / operator/ — 기간과 틱 수의 곱/나눗셈.
  • operator% — 기간 간 나머지.

예제 (Example)

#include <chrono>
#include <iostream>

int main()
{
    using namespace std::chrono;
    std::cout << (seconds{1} + milliseconds{500}).count() << '\n'; // 1500ms
    std::cout << (2 * seconds{5}).count() << '\n';                 // 10s
}

더 알아보기 (Learn more)

cppreference