mcpp index

compat.safeint

#include

Header-only checked integer arithmetic for C++ and C

#include <SafeInt.hpp>
mcpp add compat.safeint@3.0.28a

用法

来自本仓库自己的测试项目 —— CI 真的编译并运行过,不是为了网站写的。

// compat.safeint — assert the C++ class's normal arithmetic, non-throwing cast
// check, and both overflow and divide-by-zero exception paths.
#include <SafeInt.hpp>

#include <cstdint>
#include <cstdio>
#include <limits>

int main() {
    bool ok = true;
    auto check = [&](bool condition, const char* what) {
        if (!condition) {
            std::printf("FAIL: %s\n", what);
            ok = false;
        }
    };

    SafeInt<std::int32_t> lhs = 40;
    SafeInt<std::int32_t> rhs = 2;
    check(static_cast<std::int32_t>(lhs + rhs) == 42,
          "SafeInt addition");
    check(static_cast<std::int32_t>((lhs + rhs) * 2) == 84,
// …

tests/examples/safeint/tests/safeint.cpp · 项目 tests/examples/safeint

构建

targets
safeint (lib)
language
c++23
C standard
c11
import std
no
include dirs
*

源文件 (1)

  • mcpp_generated/safeint_anchor.c

生成的文件

  • mcpp_generated/safeint_anchor.c

版本

版本平台 镜像sha256
3.0.28a 最新 LinuxWindowsmacOS 源站 9e652d065a3c…