operator+,-,*,/,% (std::chrono::duration)
|
template
<
class
Rep1,
class
Period1,
class
Rep2,
class
Period2
>
typename
std::
common_type
<
duration
<
Rep1,Period1
>
, duration
<
Rep2,Period2
>>
::
type
|
(1) | (C++11부터) |
|
template
<
class
Rep1,
class
Period1,
class
Rep2,
class
Period2
>
typename
std::
common_type
<
duration
<
Rep1,Period1
>
, duration
<
Rep2,Period2
>>
::
type
|
(2) | (C++11 이후) |
|
template
<
class
Rep1,
class
Period,
class
Rep2
>
duration
<
typename
std::
common_type
<
Rep1,Rep2
>
::
type
, Period
>
|
(3) | (C++11부터) |
|
template
<
class
Rep1,
class
Rep2,
class
Period
>
duration
<
typename
std::
common_type
<
Rep1,Rep2
>
::
type
, Period
>
|
(4) | (C++11 이후) |
|
template
<
class
Rep1,
class
Period,
class
Rep2
>
duration
<
typename
std::
common_type
<
Rep1,Rep2
>
::
type
, Period
>
|
(5) | (C++11 이후) |
|
template
<
class
Rep1,
class
Period1,
class
Rep2,
class
Period2
>
typename
std::
common_type
<
Rep1,Rep2
>
::
type
|
(6) | (C++11 이후) |
|
template
<
class
Rep1,
class
Period,
class
Rep2
>
duration
<
typename
std::
common_type
<
Rep1,Rep2
>
::
type
, Period
>
|
(7) | (C++11 이후) |
|
template
<
class
Rep1,
class
Period1,
class
Rep2,
class
Period2
>
typename
std::
common_type
<
duration
<
Rep1,Period1
>
, duration
<
Rep2,Period2
>>
::
type
|
(8) | (C++11 이후) |
두 duration 간 또는 duration과 틱 카운트 간의 기본 산술 연산을 수행합니다.
Rep1
와
Rep2
의 공통 타입인
rep
를 가지는 타입으로 변환하고, 변환 후의 틱 수에
s
를 곱합니다.
이 오버로드들은
s
가
typename
std::
common_type
<
Rep1, Rep2
>
::
type
로 변환 가능할 때만 오버로드 해결에 참여합니다.
Rep1
와
Rep2
사이의 공통 타입인
rep
를 가지는 지속 시간으로 변환하고, 변환 후의 틱 수를
s
로 나눕니다. 이 오버로드는
s
가
typename
std::
common_type
<
Rep1, Rep2
>
::
type
로 변환 가능하고,
Rep2
가
duration
의 특수화가 아닌 경우에만 오버로드 해결에 참여합니다.
Rep1
와
Rep2
의 공통 타입인
rep
를 가지는 지속 시간으로 변환하고, 변환 후 틱 카운트를
s
로 나눈 나머지를 틱 카운트로 가지는 지속 시간을 생성합니다. 이 오버로드는
s
가
typename
std::
common_type
<
Rep1, Rep2
>
::
type
로 변환 가능하고,
Rep2
가
duration
의 특수화가 아닌 경우에만 오버로드 해결에 참여합니다.
목차 |
매개변수
| lhs | - | 연산자 좌변의 duration |
| rhs | - | 연산자 우변의 duration |
| d | - | 혼합 인자 연산자를 위한 duration 인자 |
| s | - | 혼합 인자 연산자를 위한 비-duration 인자 |
반환값
가정하건대 CD 가 함수 반환 타입이고 CD < A, B > = std:: common_type < A, B > :: type 라면, 다음과 같습니다:
예제
#include <chrono> #include <iostream> int main() { // 단순 산술 연산: std::chrono::seconds s = std::chrono::hours(1) + 2 * std::chrono::minutes(10) + std::chrono::seconds(70) / 10; std::cout << "1 hour + 2*10 min + 70/10 sec = " << s << " (seconds)\n"; using namespace std::chrono_literals; // duration을 숫자로 나누는 것과 // duration을 다른 duration으로 나누는 것의 차이: std::cout << "Dividing that by 2 minutes gives " << s / 2min << '\n' << "Dividing that by 2 gives " << (s / 2).count() << " seconds\n"; // 나머지 연산자는 특정 duration이 시간 프레임 내에서 // 어디에 위치하는지 결정하는 데 유용합니다, 예를 들어 // 시간, 분, 초로 분해할 때: std::cout << s << " (seconds) = " << std::chrono::duration_cast<std::chrono::hours>( s) << " (hour) + " << std::chrono::duration_cast<std::chrono::minutes>( s % 1h) << " (minutes) + " << std::chrono::duration_cast<std::chrono::seconds>( s % 1min) << " (seconds)\n"; constexpr auto sun_earth_distance{150'000'000ULL}; // km constexpr auto speed_of_light{300000ULL}; // km/sec std::chrono::seconds t(sun_earth_distance / speed_of_light); // sec std::cout << "A photon flies from the Sun to the Earth in " << t / 1min << " minutes " << t % 1min << " (seconds)\n"; }
출력:
1 hour + 2*10 min + 70/10 sec = 4807s (seconds) Dividing that by 2 minutes gives 40 Dividing that by 2 gives 2403 seconds 4807s (seconds) = 1h (hour) + 20min (minutes) + 7s (seconds) A photon flies from the Sun to the Earth in 8 minutes 20s (seconds)
결함 보고서
다음 동작 변경 결함 보고서는 이전에 게시된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 3050 | C++11 | 변환 가능성 제약 조건이 비-const xvalue를 사용함 | const lvalue를 대신 사용 |