casting - How to safely cast integer in zig - Stack Overflow

The @intCast() causes undefined behavior when the target type cannot hold the resulting value. Is there

The @intCast() causes undefined behavior when the target type cannot hold the resulting value. Is there something like @safeIntCast() or a similar function which would return CastError!anytype?

The @intCast() causes undefined behavior when the target type cannot hold the resulting value. Is there something like @safeIntCast() or a similar function which would return CastError!anytype?

Share Improve this question asked Nov 17, 2024 at 13:46 VerfinVerfin 256 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

std.math has a number of functions for safe casting:

  • pub fn cast(comptime T: type, x: anytype) ?T

Cast an integer to a different integer type. If the value doesn't fit, return null.

  • pub fn lossyCast(comptime T: type, value: anytype) T

Cast a value to a different type. If the value doesn't fit in, or can't be perfectly represented by, the new type, it will be converted to the closest possible representation.

  • pub fn negateCast(x: anytype) !std.meta.Int(.signed, @bitSizeOf(@TypeOf(x)))

Returns the negation of the integer parameter. Result is a signed integer.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745632337a4637183.html

相关推荐

  • casting - How to safely cast integer in zig - Stack Overflow

    The @intCast() causes undefined behavior when the target type cannot hold the resulting value. Is there

    22天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信