0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Zig 0.10.1から0.11.0-devのオーバーフロー検知について

Posted at

@addWithOverflow@subWithOverflowについて

この関数は、オーバーフローやアンダーフローがおきそうな場合に用いる関数である

0.10.1

定義

@addWithOverflow(comptime T: type, a: T, b: T, result: *T) bool

addの結果をresultに格納し、判定をreturnのところで確認している

使い方

var flag: bool = false;
var result: u64 = undefined;
flag = @addWithOverflow(u64, 1, 2, &result);

0.11.0-dev

定義

@addWithOverflow(a: anytype, b: anytype) struct { @TypeOf(a, b), u1 }

使い方

var result: struct { u64, u1 } = @addWithOverflow(1, 2);

result[0]がaddの結果である。result[1]は0か1が入る。

違い

0.10.1と違うところは0.10.1はbool値であるが0.11.0-devはu1であるところである

さいごに

詳細は公式ドキュメントを確認してください

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?