Namespaces
Variants

ckd_add

From cppreference.net
헤더 파일에 정의됨 <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 의 너비에 맞게 래핑된 값입니다.

목차

매개변수

a, b - 정수 값
result - 결과가 저장될 주소

반환값

false * result 에 할당된 값이 덧셈의 수학적 결과를 올바르게 나타내는 경우, true 그렇지 않은 경우.

참고

함수 템플릿 ckd_add type-generic macro 에 명시된 동일한 이름을 가진 대응물과 동일한 의미론을 가지며, 이는 C23 에서 규정됩니다.

각각의 타입 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)

참조문헌

  • C++26 표준 (ISO/IEC 14882:2026):
  • 29.11.2 Checked integer operations

참고 항목

(C++26)
두 정수에 대한 검증된 뺄셈 연산
(함수 템플릿)
(C++26)
두 정수에 대한 검증된 곱셈 연산
(함수 템플릿)
C documentation for ckd_add