Namespaces
Variants

ckd_sub

From ko.cppreference.net
헤더 파일에 정의됨 <stdckdint.h>
template < class type1, class type2, class type3 >
bool ckd_sub ( type1 * result, type2 a, type3 b ) ;
(C++26부터)

x - y 의 뺄셈을 계산하고 결과를 * result 에 저장합니다. 뺄셈은 두 피연산자가 무한 범위의 부호 있는 정수 타입으로 표현된 것처럼 수행되며, 결과는 이 정수 타입에서 type1 로 변환됩니다. * result 에 할당된 값이 연산의 수학적 결과를 올바르게 나타내면 false 를 반환합니다. 그렇지 않으면 true 를 반환합니다. 이 경우 * result 에 할당된 값은 연산의 수학적 결과가 * result 의 너비로 래핑된 값입니다.

목차

매개변수

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

반환값

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

참고

함수 템플릿 ckd_sub 는 type-generic macro 에 지정된 동일한 이름을 가진 해당 C23 과 동일한 의미론을 가집니다.

각각의 타입 type1 , type2 , 그리고 type3 는 cv-unqualified 부호 있는 또는 부호 없는 정수 타입입니다.

type2 또는 type3 가 적절한 정수 타입이 아니거나, * result 가 적절한 정수 타입의 수정 가능한 lvalue가 아닌 경우 진단 메시지를 생성하는 것이 권장됩니다.

예제

#include <cstdint>
#include <limits>
#include <print>
#include <stdckdint.h>

int main()
{
    int16_t result1{};
    constexpr std::int8_t x1{-2};
    constexpr std::int16_t y1{1};
    const bool underflow1{ckd_sub(&result1, x1, y1)};
    std::println("{} - {} == {} ({})", x1, y1, result1, underflow1 ? "Underflow" : "OK");

    int16_t result2{};
    constexpr std::int8_t x2{-2};
    constexpr std::int16_t y2{std::numeric_limits<std::int16_t>::max()};
    const bool underflow2{ckd_sub(&result2, x2, y2)};
    std::println("{} - {} == {} ({})", x2, y2, result2, underflow2 ? "Underflow" : "OK");

    int32_t result3{};
    constexpr std::int8_t x3{-2};
    constexpr std::int16_t y3{std::numeric_limits<std::int16_t>::max()};
    const bool underflow3{ckd_sub(&result3, x3, y3)};
    std::println("{} - {} == {} ({})", x3, y3, result3, underflow3 ? "Underflow" : "OK");
}

출력:

-2 - 1 == -3 (OK)
-2 - 32767 == 32767 (Underflow)
-2 - 32767 == -32769 (OK)

참고문헌

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

참고 항목

(C++26)
두 정수에 대한 검사된 덧셈 연산
(함수 템플릿)
(C++26)
두 정수에 대한 검사된 곱셈 연산
(함수 템플릿)
C 문서 for ckd_sub