Aliases for the checkedint module using IntFlagPolicy.asserts.
Common error signaling facilities for the checkedint package.
Aliases for the checkedint module using IntFlagPolicy.sticky.
Test the correctness and performance of the checkedint package.
Aliases for the checkedint module using IntFlagPolicy.throws.
Templates to facilitate treating checkedint.SmartInt and checkedint.SafeInt like the built-in numeric types in generic code.
Get the value of num as a SafeInt!N. The integral type N can be infered from the argument.
Get the value of num as a SmartInt!N. The integral type N can be infered from the argument.
Get a view or copy of num that supports bitwise operations.
Get a view or copy of num as a basic scalar.
Cast num to a basic type suitable for indexing an array.
Wrapper for any basic integral type N that uses the checked operations from safeOp and rejects attempts to directly assign values that cannot be proven to be within the range representable by N. (checkedint.to() can be used to safely assign values of incompatible types, with runtime bounds checking.)
Wrapper for any basic integral type N that uses the checked operations from smartOp and bounds checks assignments with checkedint.to().
Aliases to the basic scalar type associated with T, assuming either:
Otherwise, BasicScalar aliases to void.
template alias that evaluates to SafeInt!(N, policy, bitOps) in debug mode, and N in release mode. This way, SafeInt!N is used to debug integer logic while testing, but the basic N is used in release mode for maximum speed and the smallest binaries.
Wrapper for any basic integral type N that uses the checked operations from safeOp and rejects attempts to directly assign values that cannot be proven to be within the range representable by N. (checkedint.to() can be used to safely assign values of incompatible types, with runtime bounds checking.)
Wrapper for any basic integral type N that uses the checked operations from smartOp and bounds checks assignments with checkedint.to().
Evaluates to true if either:
And bitwise operators such as << and ~ are available for T.
Implements various integer math operations with error checking.
Implements various integer math operations with error checking.
A wrapper for std.conv.to() which uses checkedint.flags for error signaling when converting between any combination of basic scalar types and checkedint types. With an appropriate policy, this allows checkedint.to() to be used for numeric conversions in pure nothrow code, unlike std.conv.to().
Checked integer arithmetic operations, functions, and types with improved handling of errors and corner cases compared to the basic integral types.
Note: Normally this module should not be imported directly. Instead, import one of checkedint.throws, checkedint.asserts, or checkedint.sticky, depending on which error signalling policy is needed. (See below.)
$(BIG $(B Problems solved by `checkedint`))
As in many other programming languages (C, C++, Java, etc.) D's basic integral types (such as int or ulong) are surprisingly difficult to use correctly in the general case, due to variuos departures from the behaviour of ideal mathematical integers:
The checkedint package offers solutions to all of these issues and more.
$(BIG $(B `SafeInt` versus `SmartInt`))
Two different approaches are available:
There is no meaningful performance difference between SafeInt and SmartInt. For general use, choosing SmartInt simplifies code and maximizes the range of inputs accepted.
SafeInt is intended mainly as a debugging tool, to help identify problems in code that must also work correctly with the basic integral types. The DebugInt template alias makes it simple to use of SafeInt in debug builds, and raw basic types in release builds.
$(BIG $(B Error Signaling))
Some types of problem are signaled by a compile-time error, others at runtime. Runtime signaling is done through checkedint.flags. Three different runtime signalling policies are available:
In normal code, there is no performance penalty for allowing checkedint to throw. Doing so is highly recommended because this makes it easier to use correctly, and yields more precise error messages when something goes wrong.
$(BIG $(B Generic Code))
The checkedint.traits module provides checkedint-aware versions of various numerical type traits from std.traits, such as Signed, isSigned and isIntegral. This allows writing generic algorithms that work with any of SmartInt, SafeInt, and the built-in numeric types such as uint and long.
Also of note is the idx() function, which concisely and safely casts from any integral type (built-in, SmartInt, or SafeInt) to either size_t or ptrdiff_t for easy array indexing.
$(BIG $(B Performance))
Replacing all basic integer types with SmartInt or SafeInt will slow down exectuion somewhat. How much depends on many factors, but for most code following a few simple rules should keep the penalty low:
The above guidelines should be more than sufficient for most programs. But, some micro-optimization are possible as well, if needed:
References: core._checkedint