Movatterモバイル変換


[0]ホーム

URL:


D Logo
Menu
Search

Library Reference

version 2.112.0

overview

Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.Requires a signed-in GitHub account. This works well for small changes.If you'd like to make larger changes you may want to consider usinga local clone.

std.checkedint

This module defines facilities for efficient checking of integral operationsagainst overflow, casting with loss of precision, unexpected change of sign,etc. The checking (and possibly correction) can be done at operation level, forexampleopChecked!"+"(x, y, overflow) adds two integralsx andy and setsoverflow totrue if an overflow occurred. The flagoverflow(abool passed by reference) is not touched if the operation succeeded, so thesame flag can be reused for a sequence of operations and tested at the end.
Issuing individual checked operations is flexible and efficient but oftentedious. TheChecked facility offers encapsulated integral wrappers thatdo all checking internally and have configurable behavior upon erroneousresults. For example,Checked!int is a type that behaves likeint but abortsexecution immediately whenever involved in an operation that produces thearithmetically wrong result. The accompanying convenience functionchecked uses type deduction to convert a valuex of integral typeT toChecked!T by means ofchecked(x). For example:
void main(){import std.checkedint, std.stdio;    writeln((checked(5) + 7).get);// 12    writeln((checked(10) * 1000 * 1000 * 1000).get);// Overflow}
Similarly,checked(-1) > uint(0) aborts execution (even though the built-incomparisonint(-1) > uint(0) is surprisingly true due to language'sconversion rules modeled after C). Thus,Checked!int is a virtually drop-inreplacement forint useable in debug builds, to be replaced byint inrelease mode if efficiency demands it.
Checked has customizable behavior with the help of a second type parameter,Hook. Depending on what methodsHook defines, core operations on theunderlying integral may be verified for overflow or completely redefined. IfHook defines no method at all and carries no state, there is no change inbehavior, i.e.Checked!(int, void) is a wrapper aroundint that adds nocustomization at all.
This module provides a few predefined hooks (below) that add useful behavior toChecked:
Abort fails every incorrect operation with a message tostd.stdio. stderr followed by a call toassert(0). It is the default second parameter, i.e.Checked!short is the same asChecked!(short, Abort).
Throw fails every incorrect operation by throwing an exception.
Warn prints incorrect operations tostd.stdio.stderr but otherwise preserves the built-in behavior.
ProperCompare fixes the comparison operators==,!=,<,<=,>, and>= to return correct results in all circumstances, at a slight cost in efficiency. For example,Checked!(uint, ProperCompare)(1) > -1 istrue, which is not the case for the built-in comparison. Also, comparing numbers for equality with floating-point numbers only passes if the integral can be converted to the floating-point number precisely, so as to preserve transitivity of equality.
WithNaN reserves a special "Not a Number" (NaN) value akin to the homonym value reserved for floating-point values. Once aChecked!(X, WithNaN) gets this special value, it preserves and propagates it until reassigned.isNaN can be used to query whether the object is not a number.
Saturate implements saturating arithmetic, i.e.Checked!(int, Saturate) "stops" atint.max for all operations that would cause anint to overflow toward infinity, and atint.min for all operations that would correspondingly overflow toward negative infinity.
These policies may be used alone, e.g.Checked!(uint, WithNaN) defines auint-like type that reaches a stable NaN state for all erroneous operations.They may also be "stacked" on top of each other, owing to the property that achecked integral emulates an actual integral, which means another checkedintegral can be built on top of it. Some combinations of interest include:
Checked!(Checked!int, ProperCompare)
defines anint with fixedcomparison operators that will fail withassert(0) upon overflow. (Recall thatAbort is the default policy.) The order in which policies are combined isimportant because the outermost policy (ProperCompare in this case) has thefirst crack at intercepting an operator. The converse combinationChecked!(Checked!(int, ProperCompare)) is meaningless becauseAbort willintercept comparison and will fail without givingProperCompare a chance tointervene.
Checked!(Checked!(int, ProperCompare), WithNaN)
defines anint-liketype that supports a NaN value. For values that are not NaN, comparison worksproperly. Again the composition order is important;Checked!(Checked!(int,WithNaN), ProperCompare) does not have good semantics becauseProperCompareintercepts comparisons before the numbers involved are tested for NaN.
The hook's members are looked up statically in a Design by Introspection mannerand are all optional. The table below illustrates the members that a hook typemay define and their influence over the behavior of theChecked type using it.In the table,hook is an alias forHook if the typeHook does notintroduce any state, or an object of typeHook otherwise.
Hook memberSemantics inChecked!(T, Hook)
defaultValueIf defined,Hook.defaultValue!T is used as thedefault initializer of the payload.
minIf defined,Hook.min!T is used as the minimum value ofthe payload.
maxIf defined,Hook.max!T is used as the maximum value ofthe payload.
hookOpCastIf defined,hook.hookOpCast!U(get) is forwardedto unconditionally when the payload is to be cast to typeU.
onBadCastIf defined andhookOpCast isnot defined,onBadCast!U(get) is forwarded to when the payload is to be cast to typeUand the cast would lose information or force a change of sign.
hookOpEqualsIf defined,hook.hookOpEquals(get, rhs) isforwarded to unconditionally when the payload is compared for equality againstvaluerhs of integral, floating point, or Boolean type.
hookOpCmpIf defined,hook.hookOpCmp(get, rhs) isforwarded to unconditionally when the payload is compared for ordering againstvaluerhs of integral, floating point, or Boolean type.
hookOpUnaryIf defined,hook.hookOpUnary!op(get) (whereopis the operator symbol) is forwarded to for unary operators- and~. Inaddition, for unary operators++ and--,hook.hookOpUnary!op(payload) iscalled, wherepayload is a reference to the value wrapped byChecked so thehook can change it.
hookOpBinaryIf defined,hook.hookOpBinary!op(get, rhs)(whereop is the operator symbol andrhs is the right-hand side operand) isforwarded to unconditionally for binary operators+,-,*,/,%,^^,&,|,^,<<,>>, and>>>.
hookOpBinaryRightIf defined,hook.hookOpBinaryRight!op(lhs, get) (whereop is the operator symbol andlhs is the left-hand side operand) is forwarded to unconditionally for binaryoperators+,-,*,/,%,^^,&,|,^,<<,>>, and>>>.
onOverflowIf defined,hook.onOverflow!op(get) is forwardedto for unary operators that overflow but only ifhookOpUnary is not defined.Unary~ does not overflow; unary- overflows only when the most negativevalue of a signed type is negated, and the result of the hook call is returned.When the increment or decrement operators overflow, the payload is assigned theresult ofhook.onOverflow!op(get). When a binary operator overflows, theresult ofhook.onOverflow!op(get, rhs) is returned, but only ifHook doesnot definehookOpBinary.
hookOpOpAssignIf defined,hook.hookOpOpAssign!op(payload,rhs) (whereop is the operator symbol andrhs is the right-hand sideoperand) is forwarded to unconditionally for binary operators+=,-=,*=,/=,%=,^^=,&=,|=,^=,<<=,>>=, and>>>=.
onLowerBoundIf defined,hook.onLowerBound(value, bound)(wherevalue is the value being assigned) is forwarded to when the result ofbinary operators+=,-=,*=,/=,%=,^^=,&=,|=,^=,<<=,>>=,and>>>= is smaller than the smallest value representable byT.
onUpperBoundIf defined,hook.onUpperBound(value, bound)(wherevalue is the value being assigned) is forwarded to when the result ofbinary operators+=,-=,*=,/=,%=,^^=,&=,|=,^=,<<=,>>=,and>>>= is larger than the largest value representable byT.
hookToHashIf defined,hook.hookToHash(payload)(wherepayload is a reference to the value wrapped by Checked) is forwardedto whentoHash is called on a Checked type. Custom hashing can be implementedin aHook, otherwise the built-in hashing is used.

Sourcestd/checkedint.d

Examples:
int[] concatAndAdd(int[] a,int[] b,int offset){// Aborts on overflow on size computationauto r =newint[(checked(a.length) + b.length).get];// Aborts on overflow on element computationforeach (i; 0 .. a.length)        r[i] = (a[i] + checked(offset)).get;foreach (i; 0 .. b.length)        r[i + a.length] = (b[i] + checked(offset)).get;return r;}writeln(concatAndAdd([1, 2, 3], [4, 5], -1));// [0, 1, 2, 3, 4]
Examples:
Saturate stops at an overflow
auto x = (cast(byte) 127).checked!Saturate;writeln(x);// 127x++;writeln(x);// 127
Examples:
WithNaN has a special "Not a Number" (NaN) value akin to the homonym value reserved for floating-point values
auto x = 100.checked!WithNaN;writeln(x);// 100x /= 0;assert(x.isNaN);
Examples:
ProperCompare fixes the comparison operators ==, !=, <, <=, >, and >= to return correct results
uint x = 1;auto y = x.checked!ProperCompare;assert(x < -1);// built-in comparisonassert(y > -1);// ProperCompare
Examples:
Throw fails every incorrect operation by throwing an exception
import std.exception : assertThrown;auto x = -1.checked!Throw;assertThrown(x / 0);assertThrown(x +int.min);assertThrown(x ==uint.max);
structChecked(T, Hook = Abort) if (isIntegral!T || is(T ==Checked!(U, H), U, H));
Checked integral type wraps an integralT and customizes its behavior with thehelp of aHook type. The type wrapped must be one of the predefined integrals(unqualified), or another instance ofChecked.
Parameters:
Ttype that is wrapped in theChecked type
Hookhook type that customizes the behavior of theChecked type
Examples:
// Hook that ignores all problems.staticstruct Ignore{    @nogcnothrowpure @safestatic:    Dst onBadCast(Dst, Src)(Src src) {returncast(Dst) src; }    Lhs onLowerBound(Rhs, T)(Rhs rhs, T bound) {returncast(T) rhs; }    T onUpperBound(Rhs, T)(Rhs rhs, T bound) {returncast(T) rhs; }bool hookOpEquals(Lhs, Rhs)(Lhs lhs, Rhs rhs) {return lhs == rhs; }int hookOpCmp(Lhs, Rhs)(Lhs lhs, Rhs rhs) {return (lhs > rhs) - (lhs < rhs); }typeof(~Lhs()) onOverflow(string x, Lhs)(ref Lhs lhs) {returnmixin(x ~"lhs"); }typeof(Lhs() + Rhs()) onOverflow(string x, Lhs, Rhs)(Lhs lhs, Rhs rhs)    {staticif (x =="/")returntypeof(lhs / rhs).min;elsereturnmixin("lhs" ~ x ~"rhs");    }}auto x =Checked!(int, Ignore)(5) + 7;
aliasRepresentation = T;
The type of the integral subject to checking.
Hookhook;
hook is a member variable if it has state, or an alias forHook otherwise.
autoget() inout;
Returns:
A copy of the underlying value.
Examples:
auto x = checked(ubyte(42));staticassert(is(typeof(x.get()) ==ubyte));writeln(x.get);// 42const y = checked(ubyte(42));staticassert(is(typeof(y.get()) ==constubyte));writeln(y.get);// 42
enum Checked!(T, Hook)min;

enum Checked!(T, Hook)max;
Defines the minimum and maximum. These values are hookable by definingHook.min and/orHook.max.
Examples:
Defines the minimum and maximum. These values are hookable by definingHook.min and/orHook.max.
writeln(Checked!short.min);// -32768writeln(Checked!(short, WithNaN).min);// -32767writeln(Checked!(uint, WithNaN).max);// uint.max - 1
this(U)(Urhs)
if (valueConvertible!(U, T) || !isIntegral!T && is(typeof(T(rhs))) || is(U == Checked!(V, W), V, W) && is(typeof(Checked!(T, Hook)(rhs.get))));
Constructor taking a value properly convertible to the underlying type.U may be either an integral that can be converted toT without a loss, or anotherChecked instance whose representation may be in turn converted toT without a loss.
Examples:
auto a = checked(42L);writeln(a);// 42auto b = Checked!long(4242);// convert 4242 to longwriteln(b);// 4242
ref CheckedopAssign(U)(Urhs) return
if (is(typeof(Checked!(T, Hook)(rhs))));
Assignment operator. Has the same constraints as the constructor.
Parameters:
UrhsThe value to assign
Returns:
A reference tothis
Examples:
Checked!long a;a = 42L;writeln(a);// 42a = 4242;writeln(a);// 4242
Examples:
Checked!long a, b;a = b = 3;assert(a == 3 && b == 3);
this(Range)(Rangestr)
if (isInputRange!Range && isSomeChar!(ElementType!Range));
Construct from a decimal string. The conversion follows the same rules asstd.conv.to converting a string to the wrappedT type.
Parameters:
Rangestraninput range of characters
Examples:
std.conv.to can convert a string to aChecked!T:
import std.conv : to;const a = to!long("1234");const b = to!(Checked!long)("1234");writeln(a);// b
UopCast(U, this _)()
if (isIntegral!U || isFloatingPoint!U || is(U == bool));
Casting operator to integral,bool, or floating point type.
If a cast to a floating-point type is requested andHook definesonBadCast, the cast is verified by ensuringget == cast(T) U(get). If that is nottrue,hook.onBadCast!U(get) is returned.
If a cast to an integral type is requested andHook definesonBadCast, the cast is verified by ensuringget andcast(U) get are the same arithmetic number. (Note thatint(-1) anduint(1) are different values arithmetically although they have the same bitwise representation and compare equal by language rules.) If the numbers are not arithmetically equal,hook.onBadCast!U(get) is returned.
Parameters:
UThe type to cast to
Returns:
IfHook defineshookOpCast, the call immediately returnshook.hookOpCast!U(get). Otherwise, casting tobool yields get != 0 and casting to another integral that can represent all values ofT returnsget promoted toU.
Examples:
writeln(cast(uint)checked(42));// 42writeln(cast(uint)checked!WithNaN(-42));// uint.max
boolopEquals(U, this _)(Urhs)
if (isIntegral!U || isFloatingPoint!U || is(U == bool) || is(U == Checked!(V, W), V, W) && is(typeof(this ==rhs.payload)));
Comparesthis againstrhs for equality.
IfU is also an instance ofChecked, both hooks (left- and right-hand side) are introspected for the methodhookOpEquals. If both define it, priority is given to the left-hand side.
Parameters:
UrhsRight-hand side to compare for equality
Returns:
IfHook defineshookOpEquals, the function forwards to hook.hookOpEquals(get, rhs). Otherwise, the result of the built-in operationget == rhs is returned.
Examples:
import std.traits : isUnsigned;staticstruct MyHook{staticbool thereWereErrors;staticbool hookOpEquals(L, R)(L lhs, Rrhs)    {if (lhs !=rhs)returnfalse;staticif (isUnsigned!L && !isUnsigned!R)        {if (lhs > 0 &&rhs < 0) thereWereErrors =true;        }elsestaticif (isUnsigned!R && !isUnsigned!L)if (lhs < 0 &&rhs > 0) thereWereErrors =true;// Preserve built-in behavior.returntrue;    }}auto a = checked!MyHook(-42);writeln(a);// uint(-42)assert(MyHook.thereWereErrors);MyHook.thereWereErrors =false;writeln(checked!MyHook(uint(-42)));// -42assert(MyHook.thereWereErrors);staticstruct MyHook2{staticbool hookOpEquals(L, R)(L lhs, Rrhs)    {return lhs ==rhs;    }}MyHook.thereWereErrors =false;writeln(checked!MyHook2(uint(-42)));// a// Hook on left hand side takes precedence, so no errorsassert(!MyHook.thereWereErrors);
nothrow @safe size_ttoHash() const;

nothrow @safe size_ttoHash(this _)() shared const;
Generates a hash forthis. IfHook defineshookToHash, the call immediately returnshook.hookToHash(payload). IfHook does not implementhookToHash, but it has state, a hash will be generated for theHook using the built-in function and it will be xored with the hash of thepayload.
Returns:
The hash ofthis instance.
voidtoString(Writer, Char)(ref scope Writersink, ref scope const FormatSpec!Charfmt) const;
Writes a string representation of this to asink.
Parameters:
WritersinkAChar acceptingoutput range.
FormatSpec!CharfmtAstd.format.FormatSpec which controls how this is formatted.
Examples:
toString is rarely directly invoked; the usual way of using it is viastd.format.format:
import std.format;writeln(format("%04d", checked(15)));// "0015"writeln(format("0x%02x", checked(15)));// "0x0f"
autoopCmp(U, this _)(const Urhs)
if (isIntegral!U || isFloatingPoint!U || is(U == bool));

autoopCmp(U, Hook1, this _)(Checked!(U, Hook1)rhs);
Comparesthis againstrhs for ordering. IfHook defineshookOpCmp, the function forwards tohook.hookOpCmp(get, rhs). Otherwise, the result of the built-in comparison operation is returned.
IfU is also an instance ofChecked, both hooks (left- and right-hand side) are introspected for the methodhookOpCmp. If both define it, priority is given to the left-hand side.
Parameters:
UrhsThe right-hand side operand
Ueither the type ofrhs or the underlying type ifrhs is aChecked instance
Hook1Ifrhs is aChecked instance,Hook1 represents the instance's behavior hook
Returns:
The result ofhookOpCmp ifhook defineshookOpCmp. IfU is an instance ofChecked andhook does not definehookOpCmp, result ofrhs.hook.hookOpCmp is returned. If none of the instances specify the behavior viahookOpCmp,-1 is returned iflhs is lesser thanrhs,1 iflhs is greater thanrhs and0 on equality.
Examples:
import std.traits : isUnsigned;staticstruct MyHook{staticbool thereWereErrors;staticint hookOpCmp(L, R)(L lhs, Rrhs)    {staticif (isUnsigned!L && !isUnsigned!R)        {if (rhs < 0 &&rhs >= lhs)                thereWereErrors =true;        }elsestaticif (isUnsigned!R && !isUnsigned!L)        {if (lhs < 0 && lhs >=rhs)                thereWereErrors =true;        }// Preserve built-in behavior.return lhs <rhs ? -1 : lhs >rhs;    }}auto a = checked!MyHook(-42);assert(a >uint(42));assert(MyHook.thereWereErrors);staticstruct MyHook2{staticint hookOpCmp(L, R)(L lhs, Rrhs)    {// Default behaviorreturn lhs <rhs ? -1 : lhs >rhs;    }}MyHook.thereWereErrors =false;assert(Checked!(uint, MyHook2)(uint(-42)) <= a);//assert(Checked!(uint, MyHook2)(uint(-42)) >= a);// Hook on left hand side takes precedence, so no errorsassert(!MyHook.thereWereErrors);assert(a <= Checked!(uint, MyHook2)(uint(-42)));assert(MyHook.thereWereErrors);
autoopUnary(string op, this _)()
if (op == "+" || op == "-" || op == "~");

ref CheckedopUnary(string op)() return
if (op == "++" || op == "--");
Defines unary operators+,-,~,++, and--. Unary+ is not overridable and always has built-in behavior (returnsthis). For the others, ifHook defineshookOpUnary,opUnary forwards to Checked!(typeof(hook.hookOpUnary!op(get)), Hook)(hook.hookOpUnary!op(get)).
IfHook does not definehookOpUnary but definesonOverflow,opUnary forwards tohook.onOverflow!op(get) in case an overflow occurs. For++ and--, the payload is assigned from the result of the call toonOverflow.
Note that unary- is considered to overflow ifT is a signed integral of 32 or 64 bits and is equal to the most negative value. This is because that value has no positive negation.
Parameters:
opThe unary operator
Returns:
AChecked instance representing the result of the unary operation
Examples:
staticstruct MyHook{staticbool thereWereErrors;static L hookOpUnary(string x, L)(L lhs)    {if (x =="-" && lhs == -lhs) thereWereErrors =true;return -lhs;    }}auto a = checked!MyHook(long.min);writeln(a);// -aassert(MyHook.thereWereErrors);auto b = checked!void(42);writeln(++b);// 43
autoopBinary(string op, Rhs)(const Rhsrhs)
if (isIntegral!Rhs || isFloatingPoint!Rhs || is(Rhs == bool));

autoopBinary(string op, Rhs)(const Rhsrhs) const
if (isIntegral!Rhs || isFloatingPoint!Rhs || is(Rhs == bool));

autoopBinary(string op, U, Hook1)(Checked!(U, Hook1)rhs);

autoopBinary(string op, U, Hook1)(Checked!(U, Hook1)rhs) const;
Defines binary operators+,-,*,/,%,^^,&,|,^,<<,>>, and>>>. IfHook defineshookOpBinary,opBinary forwards to Checked!(typeof(hook.hookOpBinary!op(get, rhs)), Hook)(hook.hookOpBinary!op(get, rhs)).
IfHook does not definehookOpBinary but definesonOverflow,opBinary forwards tohook.onOverflow!op(get,rhs) in case an overflow occurs.
If twoChecked instances are involved in a binary operation and both definehookOpBinary, the left-hand side hook has priority. If both defineonOverflow, a compile-time error occurs.
Parameters:
opThe binary operator
RhsrhsThe right hand side operand
UIfrhs is aChecked instance,U represents the underlying instance type
Hook1Ifrhs is aChecked instance,Hook1 represents the instance's behavior hook
Returns:
AChecked instance representing the result of the binary operation
autoopBinaryRight(string op, Lhs)(const Lhslhs)
if (isIntegral!Lhs || isFloatingPoint!Lhs || is(Lhs == bool));

autoopBinaryRight(string op, Lhs)(const Lhslhs) const
if (isIntegral!Lhs || isFloatingPoint!Lhs || is(Lhs == bool));
Defines binary operators+,-,*,/,%,^^,&,|,^,<<,>>, and>>> for the case when a built-in numeric or Boolean type is on the left-hand side, and aChecked instance is on the right-hand side.
Parameters:
opThe binary operator
LhslhsThe left hand side operand
Returns:
AChecked instance representing the result of the binary operation
ref CheckedopOpAssign(string op, Rhs)(const Rhsrhs) return
if (isIntegral!Rhs || isFloatingPoint!Rhs || is(Rhs == bool));

ref CheckedopOpAssign(string op, Rhs)(const Rhsrhs) return
if (is(Rhs == Checked!(RhsT, RhsHook), RhsT, RhsHook));
Defines operators+=,-=,*=,/=,%=,^^=,&=,|=,^=,<<=,>>=, and>>>=.
IfHook defineshookOpOpAssign,opOpAssign forwards tohook.hookOpOpAssign!op(payload,rhs), wherepayload is a reference to the internally held data so the hook can change it.
Otherwise, the operator first evaluatesauto result = opBinary!op(payload, rhs).payload, which is subject to the hooks inopBinary. Then, ifresult is less thanChecked!(T, Hook).min and ifHook definesonLowerBound, the payload is assigned from hook.onLowerBound(result, min). Ifresult is greater thanChecked!(T, Hook).max and ifHook definesonUpperBound, the payload is assigned fromhook.onUpperBound(result, min).
If the right-hand side is also a Checked but with a different hook or underlying type, the hook and underlying type of this Checked takes precedence.
In all other cases, the built-in behavior is carried out.
Parameters:
opThe operator involved (without the"=", e.g."+" for"+=" etc)
RhsrhsThe right-hand side of the operator (left-hand side isthis)
Returns:
A reference tothis.
Examples:
staticstruct MyHook{staticbool thereWereErrors;static T onLowerBound(Rhs, T)(Rhsrhs, T bound)    {        thereWereErrors =true;return bound;    }static T onUpperBound(Rhs, T)(Rhsrhs, T bound)    {        thereWereErrors =true;return bound;    }}auto x = checked!MyHook(byte.min);x -= 1;assert(MyHook.thereWereErrors);MyHook.thereWereErrors =false;x =byte.max;x += 1;assert(MyHook.thereWereErrors);
Checked!(T, Hook)checked(Hook = Abort, T)(const Tvalue)
if (is(typeof(Checked!(T, Hook)(value))));
Convenience function that turns an integral into the correspondingCheckedinstance by using template argument deduction. The hook type may be specified(by defaultAbort).
Parameters:
Hooktype that customizes the behavior, by defaultAbort
Ttype represetinfg the underlying represantion of theChecked instance
Tvaluethe actual value of the representation
Returns:
AChecked instance customized by the providedHook andvalue
Examples:
staticassert(is(typeof(checked(42)) == Checked!int));writeln(checked(42));// Checked!int(42)staticassert(is(typeof(checked!WithNaN(42)) == Checked!(int, WithNaN)));writeln(checked!WithNaN(42));// Checked!(int, WithNaN)(42)
structAbort;
Force all integral errors to fail by printing an error message tostderr andthen abort the program.Abort is the default second argument forChecked.
Examples:
void test(T)(){    Checked!(int,Abort) x;    x = 42;auto x1 =cast(T) x;    writeln(x1);// 42//x1 += long(int.max);}test!short;test!(constshort);test!(immutableshort);
DstonBadCast(Dst, Src)(Srcsrc);
Called automatically upon a bad cast (one that loses precision or attempts to convert a negative value to an unsigned type). The source type isSrc and the destination type isDst.
Parameters:
SrcsrcSouce operand
Returns:
Nominally the result is the desired value of the cast operation, which will be forwarded as the result of the cast. ForAbort, the function never returns because it aborts the program.
TonLowerBound(Rhs, T)(Rhsrhs, Tbound);

TonUpperBound(Rhs, T)(Rhsrhs, Tbound);
Called automatically upon a bounds error.
Parameters:
RhsrhsThe right-hand side value in the assignment, after the operator has been evaluated
TboundThe value of the bound being violated
Returns:
Nominally the result is the desired value of the operator, which will be forwarded as result. ForAbort, the function never returns because it aborts the program.
boolhookOpEquals(Lhs, Rhs)(Lhslhs, Rhsrhs);
Called automatically upon a comparison for equality. In case of a erroneous comparison (one that would make a signed negative value appear equal to an unsigned positive value), this hook issuesassert(0) which terminates the application.
Parameters:
LhslhsThe first argument ofChecked, e.g.int if the left-hand side of the operator isChecked!int
RhsrhsThe right-hand side type involved in the operator
Returns:
Upon a correct comparison, returns the result of the comparison. Otherwise, the function terminates the application so it never returns.
inthookOpCmp(Lhs, Rhs)(Lhslhs, Rhsrhs);
Called automatically upon a comparison for ordering using one of the operators<,<=,>, or>=. In case the comparison is erroneous (i.e. it would make a signed negative value appear greater than or equal to an unsigned positive value), then application is terminated withassert(0). Otherwise, the three-state result is returned (positive iflhs > rhs, negative iflhs < rhs,0 otherwise).
Parameters:
LhslhsThe first argument ofChecked, e.g.int if the left-hand side of the operator isChecked!int
RhsrhsThe right-hand side type involved in the operator
Returns:
For correct comparisons, returns a positive integer iflhs > rhs, a negative integer iflhs < rhs,0 if the two are equal. Upon a mistaken comparison such asint(-1) < uint(0), the function never returns because it aborts the program.
typeof(~Lhs())onOverflow(string x, Lhs)(Lhslhs);

typeof(Lhs() + Rhs())onOverflow(string x, Lhs, Rhs)(Lhslhs, Rhsrhs);
Called automatically upon an overflow during a unary or binary operation.
Parameters:
xThe operator, e.g.-
LhslhsThe left-hand side (or sole) argument
RhsrhsThe right-hand side type involved in the operator
Returns:
Nominally the result is the desired value of the operator, which will be forwarded as result. ForAbort, the function never returns because it aborts the program.
structThrow;
Force all integral errors to fail by throwing an exception of typeThrow.CheckFailure. The message coming with the error is similar to the oneprinted byWarn.
Examples:
void test(T)(){    Checked!(int,Throw) x;    x = 42;auto x1 =cast(T) x;    writeln(x1);// 42    x = T.max + 1;import std.exception : assertThrown, assertNotThrown;    assertThrown(cast(T) x);    x = x.max;    assertThrown(x += 42);    assertThrown(x += 42L);    x = x.min;    assertThrown(-x);    assertThrown(x -= 42);    assertThrown(x -= 42L);    x = -1;    assertNotThrown(x == -1);    assertThrown(x ==uint(-1));    assertNotThrown(x <= -1);    assertThrown(x <=uint(-1));}test!short;test!(constshort);test!(immutableshort);
classCheckFailure:object.Exception;
Exception type thrown upon any failure.
this(T...)(stringf, Tvals);
Parameters:
stringfformat specifier
Tvalsactual values for the format specifier
DstonBadCast(Dst, Src)(Srcsrc);
Called automatically upon a bad cast (one that loses precision or attempts to convert a negative value to an unsigned type). The source type isSrc and the destination type isDst.
Parameters:
Srcsrcsource operand
Returns:
Nominally the result is the desired value of the cast operation, which will be forwarded as the result of the cast. ForThrow, the function never returns because it throws an exception.
Throws:
CheckFailure on bad cast
TonLowerBound(Rhs, T)(Rhsrhs, Tbound);

TonUpperBound(Rhs, T)(Rhsrhs, Tbound);
Called automatically upon a bounds error.
Parameters:
RhsrhsThe right-hand side value in the assignment, after the operator has been evaluated
TboundThe value of the bound being violated
Returns:
Nominally the result is the desired value of the operator, which will be forwarded as result. ForThrow, the function never returns because it throws.
Throws:
CheckFailure on overflow
boolhookOpEquals(L, R)(Llhs, Rrhs);
Called automatically upon a comparison for equality. Throws upon an erroneous comparison (one that would make a signed negative value appear equal to an unsigned positive value).
Parameters:
LlhsThe first argument ofChecked, e.g.int if the left-hand side of the operator isChecked!int
RrhsThe right-hand side type involved in the operator
Returns:
The result of the comparison.
Throws:
CheckFailure if the comparison is mathematically erroneous.
inthookOpCmp(Lhs, Rhs)(Lhslhs, Rhsrhs);
Called automatically upon a comparison for ordering using one of the operators<,<=,>, or>=. In case the comparison is erroneous (i.e. it would make a signed negative value appear greater than or equal to an unsigned positive value), throws aThrow.CheckFailure exception. Otherwise, the three-state result is returned (positive iflhs > rhs, negative iflhs < rhs,0 otherwise).
Parameters:
LhslhsThe first argument ofChecked, e.g.int if the left-hand side of the operator isChecked!int
RhsrhsThe right-hand side type involved in the operator
Returns:
For correct comparisons, returns a positive integer iflhs > rhs, a negative integer iflhs < rhs,0 if the two are equal.
Throws:
Upon a mistaken comparison such asint(-1) < uint(0), the function never returns because it throws aThrow.CheckedFailure exception.
typeof(~Lhs())onOverflow(string x, Lhs)(Lhslhs);

typeof(Lhs() + Rhs())onOverflow(string x, Lhs, Rhs)(Lhslhs, Rhsrhs);
Called automatically upon an overflow during a unary or binary operation.
Parameters:
xThe operator, e.g.-
LhslhsThe left-hand side (or sole) argument
RhsrhsThe right-hand side type involved in the operator
Returns:
Nominally the result is the desired value of the operator, which will be forwarded as result. ForThrow, the function never returns because it throws an exception.
Throws:
CheckFailure on overflow
structWarn;
Hook that prints tostderr a trace of all integral errors, without affectingdefault behavior.
Examples:
auto x = checked!Warn(42);short x1 =cast(short) x;//x += long(int.max);auto y = checked!Warn(cast(constint) 42);short y1 =cast(constbyte) y;
DstonBadCast(Dst, Src)(Srcsrc);
Called automatically upon a bad cast fromsrc to typeDst (one that loses precision or attempts to convert a negative value to an unsigned type).
Parameters:
SrcsrcThe source of the cast
DstThe target type of the cast
Returns:
cast(Dst)src
TonLowerBound(Rhs, T)(Rhsrhs, Tbound);

TonUpperBound(Rhs, T)(Rhsrhs, Tbound);
Called automatically upon a badopOpAssign call (one that loses precision or attempts to convert a negative value to an unsigned type).
Parameters:
RhsrhsThe right-hand side value in the assignment, after the operator has been evaluated
TboundThe bound being violated
Returns:
cast(T)rhs
boolhookOpEquals(Lhs, Rhs)(Lhslhs, Rhsrhs);
Called automatically upon a comparison for equality. In case of an Erroneous comparison (one that would make a signed negative value appear equal to an unsigned positive value), writes a warning message tostderr as a side effect.
Parameters:
LhslhsThe first argument ofChecked, e.g.int if the left-hand side of the operator isChecked!int
RhsrhsThe right-hand side type involved in the operator
Returns:
In all cases the function returns the built-in result oflhs == rhs.
Examples:
auto x = checked!Warn(-42);// Passeswriteln(x);// -42// Passes but prints a warning// assert(x == uint(-42));
inthookOpCmp(Lhs, Rhs)(Lhslhs, Rhsrhs);
Called automatically upon a comparison for ordering using one of the operators<,<=,>, or>=. In case the comparison is erroneous (i.e. it would make a signed negative value appear greater than or equal to an unsigned positive value), then a warning message is printed tostderr.
Parameters:
LhslhsThe first argument ofChecked, e.g.int if the left-hand side of the operator isChecked!int
RhsrhsThe right-hand side type involved in the operator
Returns:
In all cases, returnslhs < rhs ? -1 : lhs > rhs. The result is not autocorrected in case of an erroneous comparison.
Examples:
auto x = checked!Warn(-42);// Passesassert(x <= -42);// Passes but prints a warning// assert(x <= uint(-42));
typeof(~Lhs())onOverflow(string x, Lhs)(ref Lhslhs);

typeof(Lhs() + Rhs())onOverflow(string x, Lhs, Rhs)(Lhslhs, Rhsrhs);
Called automatically upon an overflow during a unary or binary operation.
Parameters:
xThe operator involved
LhsThe first argument ofChecked, e.g.int if the left-hand side of the operator isChecked!int
RhsThe right-hand side type involved in the operator
Returns:
mixin(x ~ "lhs") for unary,mixin("lhs" ~ x ~ "rhs") for binary
structProperCompare;
Hook that provides arithmetically correct comparisons for equality and ordering.Comparing an object of typeChecked!(X, ProperCompare) against anotherintegral (for equality or ordering) ensures that no surprising conversions fromsigned to unsigned integral occur before the comparison. UsingChecked!(X,ProperCompare) on either side of a comparison for equality against afloating-point number makes sure the integral can be properly converted to thefloating point type, thus making sure equality is transitive.
Examples:
alias opEqualsProper =ProperCompare.hookOpEquals;assert(opEqualsProper(42, 42));assert(opEqualsProper(42.0, 42.0));assert(opEqualsProper(42u, 42));assert(opEqualsProper(42, 42u));writeln(-1);// 4294967295uassert(!opEqualsProper(-1, 4294967295u));assert(!opEqualsProper(constuint(-1), -1));assert(!opEqualsProper(uint(-1), -1.0));writeln(3_000_000_000U);// -1_294_967_296assert(!opEqualsProper(3_000_000_000U, -1_294_967_296));
boolhookOpEquals(L, R)(Llhs, Rrhs);
Hook for== and!= that ensures comparison against integral values has the behavior expected by the usual arithmetic rules. The built-in semantics yield surprising behavior when comparing signed values against unsigned values for equality, for exampleuint.max == -1 or-1_294_967_296 == 3_000_000_000u. The callhookOpEquals(x, y) returnstrue if and only ifx andy represent the same arithmetic number.
If one of the numbers is an integral and the other is a floating-point number,hookOpEquals(x, y) returnstrue if and only if the integral can be converted exactly (without approximation) to the floating-point number. This is in order to preserve transitivity of equality: if hookOpEquals(x, y) andhookOpEquals(y, z) thenhookOpEquals(y, z), in casex,y, andz are a mix of integral and floating-point numbers.
Parameters:
LlhsThe left-hand side of the comparison for equality
RrhsThe right-hand side of the comparison for equality
Returns:
The result of the comparison,true if the values are equal
autohookOpCmp(L, R)(Llhs, Rrhs);
Hook for<,<=,>, and>= that ensures comparison against integral values has the behavior expected by the usual arithmetic rules. The built-in semantics yield surprising behavior when comparing signed values against unsigned values, for example0u < -1. The callhookOpCmp(x, y) returns-1 if and only ifx is smaller thany in abstract arithmetic sense.
If one of the numbers is an integral and the other is a floating-point number,hookOpEquals(x, y) returns a floating-point number that is-1 ifx < y,0 ifx == y,1 ifx > y, andNaN if the floating-point number isNaN.
Parameters:
LlhsThe left-hand side of the comparison for ordering
RrhsThe right-hand side of the comparison for ordering
Returns:
The result of the comparison (negative iflhs < rhs, positive if lhs > rhs,0 if the values are equal)
structWithNaN;
Hook that reserves a special value as a "Not a Number" representative. Forsigned integrals, the reserved value isT.min. For unsigned integrals, thereserved value isT.max.
The default value of aChecked!(X, WithNaN) is its NaN value, so care mustbe taken that all variables are explicitly initialized. Any arithmetic and logicoperation involving at least on NaN becomes NaN itself. All ofa == b,a < b,a > b,a <= b,a >= b yieldfalse if at least one ofa andb is NaN.
Examples:
auto x1 = Checked!(int,WithNaN)();assert(x1.isNaN);writeln(x1.get);// int.minassert(x1 != x1);assert(!(x1 < x1));assert(!(x1 > x1));assert(!(x1 == x1));++x1;assert(x1.isNaN);writeln(x1.get);// int.min--x1;assert(x1.isNaN);writeln(x1.get);// int.minx1 = 42;assert(!x1.isNaN);writeln(x1);// x1assert(x1 <= x1);assert(x1 >= x1);staticassert(x1.min ==int.min + 1);x1 +=long(int.max);
enum TdefaultValue(T);
The default value used for values not explicitly initialized. It is the NaN value, i.e.T.min for signed integrals andT.max for unsigned integrals.
enum Tmax(T);

enum Tmin(T);
The maximum value representable isT.max for signed integrals, T.max - 1 for unsigned integrals. The minimum value representable is T.min + 1 for signed integrals,0 for unsigned integrals.
LhshookOpCast(Lhs, Rhs)(Rhsrhs);
Ifrhs isWithNaN.defaultValue!Rhs, returnsWithNaN.defaultValue!Lhs. Otherwise, returnscast(Lhs) rhs.
Parameters:
Rhsrhsthe value being cast (Rhs is the first argument toChecked)
Lhsthe target type of the cast
Returns:
The result of the cast operation.
Examples:
auto x = checked!WithNaN(422);writeln((cast(ubyte)x));// 255x = checked!WithNaN(-422);writeln((cast(byte)x));// -128writeln(cast(short)x);// -422assert(cast(bool) x);x = x.init;// set back to NaNassert(x !=true);assert(x !=false);
boolhookOpEquals(Lhs, Rhs)(Lhslhs, Rhsrhs);
Returnsfalse iflhs == WithNaN.defaultValue!Lhs,lhs == rhs otherwise.
Parameters:
LhslhsThe left-hand side of the comparison (Lhs is the first argument toChecked)
RhsrhsThe right-hand side of the comparison
Returns:
lhs != WithNaN.defaultValue!Lhs &&lhs ==rhs
doublehookOpCmp(Lhs, Rhs)(Lhslhs, Rhsrhs);
Iflhs == WithNaN.defaultValue!Lhs, returnsdouble.init. Otherwise, has the same semantics as the default comparison.
Parameters:
LhslhsThe left-hand side of the comparison (Lhs is the first argument toChecked)
RhsrhsThe right-hand side of the comparison
Returns:
double.init iflhs == WitnNaN.defaultValue!Lhs,-1.0 if lhs < rhs,0.0 iflhs == rhs,1.0 iflhs > rhs.
Examples:
Checked!(int, WithNaN) x;assert(!(x < 0) && !(x > 0) && !(x == 0));x = 1;assert(x > 0 && !(x < 0) && !(x == 0));
autohookOpUnary(string x, T)(ref Tv);
Defines hooks for unary operators-,~,++, and--.
For- and~, ifv == WithNaN.defaultValue!T, returnsWithNaN.defaultValue!T. Otherwise, the semantics is the same as for the built-in operator.
For++ and--, ifv == WithNaN.defaultValue!Lhs or the operation would result in an overflow, setsv toWithNaN.defaultValue!T. Otherwise, the semantics is the same as for the built-in operator.
Parameters:
xThe operator symbol
TvThe left-hand side of the comparison (T is the first argument toChecked)
Returns:
  • Forx == "-" || x == "~": Ifv == WithNaN.defaultValue!T, the function returnsWithNaN.defaultValue!T. Otherwise it returns the normal result of the operator.
  • Forx == "++" || x == "--": The function returnsvoid.
Examples:
Checked!(int, WithNaN) x;++x;assert(x.isNaN);x = 1;assert(!x.isNaN);x = -x;++x;assert(!x.isNaN);
autohookOpBinary(string x, L, R)(Llhs, Rrhs);
Defines hooks for binary operators+,-,*,/,%,^^,&,|,^,<<,>>, and>>> for cases where aChecked object is the left-hand side operand. Iflhs == WithNaN.defaultValue!Lhs, returnsWithNaN.defaultValue!(typeof(lhs + rhs)) without evaluating the operand. Otherwise, evaluates the operand. If evaluation does not overflow, returns the result. Otherwise, returnsWithNaN.defaultValue!(typeof(lhs + rhs)).
Parameters:
xThe operator symbol
LlhsThe left-hand side operand (Lhs is the first argument toChecked)
RrhsThe right-hand side operand
Returns:
Iflhs != WithNaN.defaultValue!Lhs and the operator does not overflow, the function returns the same result as the built-in operator. In all other cases, returnsWithNaN.defaultValue!(typeof(lhs + rhs)).
Examples:
Checked!(int, WithNaN) x;assert((x + 1).isNaN);x = 100;assert(!(x + 1).isNaN);
autohookOpBinaryRight(string x, L, R)(Llhs, Rrhs);
Defines hooks for binary operators+,-,*,/,%,^^,&,|,^,<<,>>, and>>> for cases where aChecked object is the right-hand side operand. Ifrhs == WithNaN.defaultValue!Rhs, returnsWithNaN.defaultValue!(typeof(lhs + rhs)) without evaluating the operand. Otherwise, evaluates the operand. If evaluation does not overflow, returns the result. Otherwise, returnsWithNaN.defaultValue!(typeof(lhs + rhs)).
Parameters:
xThe operator symbol
LlhsThe left-hand side operand
RrhsThe right-hand side operand (Rhs is the first argument toChecked)
Returns:
Ifrhs != WithNaN.defaultValue!Rhs and the operator does not overflow, the function returns the same result as the built-in operator. In all other cases, returnsWithNaN.defaultValue!(typeof(lhs + rhs)).
Examples:
Checked!(int, WithNaN) x;assert((1 + x).isNaN);x = 100;assert(!(1 + x).isNaN);
voidhookOpOpAssign(string x, L, R)(ref Llhs, Rrhs);
Defines hooks for binary operators+=,-=,*=,/=,%=,^^=,&=,|=,^=,<<=,>>=, and>>>= for cases where aChecked object is the left-hand side operand. Iflhs == WithNaN.defaultValue!Lhs, no action is carried. Otherwise, evaluates the operand. If evaluation does not overflow and fits inLhs without loss of information or change of sign, setslhs to the result. Otherwise, setslhs toWithNaN.defaultValue!Lhs.
Parameters:
xThe operator symbol (without the=)
LlhsThe left-hand side operand (Lhs is the first argument toChecked)
RrhsThe right-hand side operand
Returns:
void
Examples:
Checked!(int, WithNaN) x;x += 4;assert(x.isNaN);x = 0;x += 4;assert(!x.isNaN);x +=int.max;assert(x.isNaN);
boolisNaN(T)(const Checked!(T, WithNaN)x);
Queries whether aChecked!(T, WithNaN) object is not a number (NaN).
Parameters:
Checked!(T, WithNaN)xtheChecked instance queried
Returns:
true ifx is a NaN,false otherwise
Examples:
auto x1 = Checked!(int, WithNaN)();assert(x1.isNaN);x1 = 1;assert(!x1.isNaN);x1 = x1.init;assert(x1.isNaN);
structSaturate;
Hook that implementssaturation, i.e. any arithmetic operation that wouldoverflow leaves the result at its extreme value (min ormax depending on thedirection of the overflow).
Saturation is not sticky; if a value reaches its saturation value, anotheroperation may take it back to normal range.
Examples:
auto x = checked!Saturate(int.max);++x;writeln(x);// int.max--x;writeln(x);// int.max - 1x =int.min;writeln(-x);// int.maxx -= 42;writeln(x);// int.minwriteln(x * -2);// int.max
TonLowerBound(Rhs, T)(Rhs, Tbound);

TonUpperBound(Rhs, T)(Rhs, Tbound);
Implements saturation for operators+=,-=,*=,/=,%=,^^=,&=,|=,^=,<<=,>>=, and>>>=. This hook is called if the result of the binary operation does not fit inLhs without loss of information or a change in sign.
Parameters:
RhsThe right-hand side type in the assignment, after the operation has been computed
TboundThe bound being violated
Returns:
Lhs.max ifrhs >= 0,Lhs.min otherwise.
Examples:
auto x = checked!Saturate(short(100));x += 33000;writeln(x);// short.maxx -= 70000;writeln(x);// short.min
autoonOverflow(string x, Lhs)(Lhs);

typeof(Lhs() + Rhs())onOverflow(string x, Lhs, Rhs)(Lhslhs, Rhsrhs);
Implements saturation for operators+,- (unary and binary),*,/,%,^^,&,|,^,<<,>>, and>>>.
For unary-,onOverflow is called iflhs == Lhs.min andLhs is a signed type. The function returnsLhs.max.
For binary operators, the result is as follows:
  • Lhs.max if the result overflows in the positive direction, on division by0, or on shifting right by a negative value
  • Lhs.min if the result overflows in the negative direction
  • 0 iflhs is being shifted left by a negative value, or shifted right by a large positive value
Parameters:
xThe operator involved in theopAssign operation
LhsThe left-hand side type of the operator (Lhs is the first argument toChecked)
RhsThe right-hand side type in the operator
Returns:
The saturated result of the operator.
Examples:
writeln(checked!Saturate(int.max) + 1);// int.maxwriteln(checked!Saturate(100)^^10);// int.maxwriteln(checked!Saturate(-100)^^10);// int.maxwriteln(checked!Saturate(100) / 0);// int.maxwriteln(checked!Saturate(100) << -1);// 0writeln(checked!Saturate(100) << 33);// int.maxwriteln(checked!Saturate(100) >> -1);// int.maxwriteln(checked!Saturate(100) >> 33);// 0
typeof(mixin(x == "cmp" ? "0" : "L() " ~ x ~ " R()"))opChecked(string x, L, R)(const Llhs, const Rrhs, ref booloverflow)
if (isIntegral!L && isIntegral!R);
Defines binary operations with overflow checking for any two integral types.The result type obeys the language rules (even when they may becounterintuitive), andoverflow is set if an overflow occurs (includinginadvertent change of signedness, e.g.-1 is converted touint).Conceptually the behavior is:
  1. Perform the operation in infinite precision
  2. If the infinite-precision result fits in the result type, return it anddo not touchoverflow
  3. Otherwise, setoverflow totrue and return an unspecified value
The implementation exploits properties of types and operations to minimizeadditional work.
Parameters:
xThe binary operator involved, e.g./
LlhsThe left-hand side of the operator
RrhsThe right-hand side of the operator
booloverflowThe overflow indicator (assignedtrue in case there's an error)
Returns:
The result of the operation, which is the same as the built-in operator
Examples:
booloverflow;assert(opChecked!"+"(constshort(1),short(1),overflow) == 2 && !overflow);assert(opChecked!"+"(1, 1,overflow) == 2 && !overflow);assert(opChecked!"+"(1, 1u,overflow) == 2 && !overflow);assert(opChecked!"+"(-1, 1u,overflow) == 0 && !overflow);assert(opChecked!"+"(1u, -1,overflow) == 0 && !overflow);
Examples:
booloverflow;assert(opChecked!"-"(1, 1,overflow) == 0 && !overflow);assert(opChecked!"-"(1, 1u,overflow) == 0 && !overflow);assert(opChecked!"-"(1u, -1,overflow) == 2 && !overflow);assert(opChecked!"-"(-1, 1u,overflow) == 0 &&overflow);
Copyright © 1999-2026 by theD Language Foundation | Page generated byDdoc on Fri Feb 20 00:53:20 2026

[8]ページ先頭

©2009-2026 Movatter.jp