ckd_add
|
헤더 파일에 정의됨
<stdckdint.h>
|
||
|
template
<
class
type1,
class
type2,
class
type3
>
bool ckd_add ( type1 * result, type2 a, type3 b ) ; |
(C++26부터) | |
덧셈 x + y 을 계산하고 결과를 * result 에 저장합니다. 덧셈은 두 피연산자가 무한한 범위의 부호 있는 정수 타입으로 표현된 것처럼 수행되며, 결과는 이 정수 타입에서 type1 으로 변환됩니다. * result 에 할당된 값이 연산의 수학적 결과를 올바르게 나타내면 false 를 반환합니다. 그렇지 않으면 true 를 반환합니다. 이 경우 * result 에 할당된 값은 연산의 수학적 결과가 * result 의 너비에 맞게 래핑된 값입니다.
목차매개변수
반환값false 가 * result 에 할당된 값이 덧셈의 수학적 결과를 올바르게 나타내는 경우, true 그렇지 않은 경우. 참고
함수 템플릿
각각의 타입 type1 , type2 , 그리고 type3 는 cv-unqualified 부호 있는 또는 부호 없는 정수 타입입니다. type2 또는 type3 가 적절한 정수 타입이 아니거나, * result 가 적절한 정수 타입의 수정 가능한 lvalue가 아닌 경우 진단 메시지를 생성하는 것이 권장됩니다. 예제이 코드 실행하기 #include <cstdint>
#include <limits>
#include <print>
#include <stdckdint.h>
int main()
{
const std::uint8_t x{14};
std::uint16_t y{28}, result1{};
bool overflow{};
overflow = ckd_add(&result1, x, y);
std::println("{} + {} => {} ({})", x, y, result1, overflow ? "Overflow" : "OK");
y = std::numeric_limits<std::uint16_t>::max();
overflow = ckd_add(&result1, x, y);
std::println("{} + {} => {} ({})", x, y, result1, overflow ? "Overflow" : "OK");
std::uint32_t result2{};
overflow = ckd_add(&result2, x, y);
std::println("{} + {} => {} ({})", x, y, result2, overflow ? "Overflow" : "OK");
}
출력: 14 + 28 => 42 (OK)
14 + 65535 => 13 (Overflow)
14 + 65535 => 65549 (OK)
참조문헌
참고 항목
| ||||||||||||