From 361b51cddf2620bdad256cbd0c5e318be4b09c14 Mon Sep 17 00:00:00 2001 From: Nathan Sashihara <21227491+n8sh@users.noreply.github.com> Date: Wed, 24 Mar 2021 09:12:31 -0700 Subject: [PATCH] Fix Issue 21760 - std.conv.to does not know how to convert a string to a std.experimental.checkedint.Checked!T --- std/experimental/checkedint.d | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/std/experimental/checkedint.d b/std/experimental/checkedint.d index 5fb501146..9c7816697 100644 --- a/std/experimental/checkedint.d +++ b/std/experimental/checkedint.d @@ -263,7 +263,8 @@ if (isIntegral!T || is(T == Checked!(U, H), U, H)) { import std.algorithm.comparison : among; import std.experimental.allocator.common : stateSize; - import std.traits : hasMember; + import std.range.primitives : isInputRange, ElementType; + import std.traits : hasMember, isSomeChar; /** The type of the integral subject to checking. @@ -378,6 +379,34 @@ if (isIntegral!T || is(T == Checked!(U, H), U, H)) assert(a == 3 && b == 3); } + /** + Construct from a decimal string. The conversion follows the same rules as + $(REF to, std, conv) converting a string to the wrapped `T` type. + + Params: + str = an $(REF_ALTTEXT input range, isInputRange, std,range,primitives) + of characters + */ + this(Range)(Range str) + if (isInputRange!Range && isSomeChar!(ElementType!Range)) + { + import std.conv : to; + + this(to!T(str)); + } + + /** + $(REF to, std, conv) can convert a string to a `Checked!T`: + */ + @system unittest + { + import std.conv : to; + + const a = to!long("1234"); + const b = to!(Checked!long)("1234"); + assert(a == b); + } + // opCast /** Casting operator to integral, `bool`, or floating point type. If `Hook`