Skip to content

Commit cecd745

Browse files
committed
Merge pull request #2015 from monarchdodra/emplaceQual
Improve emplaceRef for qualified construction
2 parents 05b60c2 + 59ad77a commit cecd745

3 files changed

Lines changed: 183 additions & 167 deletions

File tree

std/algorithm.d

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ template reduce(fun...) if (fun.length >= 1)
799799
result = void;
800800
foreach (i, T; result.Types)
801801
{
802-
emplaceRef(result[i], seed);
802+
emplaceRef!T(result[i], seed);
803803
}
804804
r.popFront();
805805
return reduce(result, r);
@@ -865,7 +865,7 @@ template reduce(fun...) if (fun.length >= 1)
865865

866866
foreach (i, T; result.Types)
867867
{
868-
emplaceRef(result[i], elem);
868+
emplaceRef!T(result[i], elem);
869869
}
870870
}
871871
}
@@ -1456,7 +1456,7 @@ void uninitializedFill(Range, Value)(Range range, Value filler)
14561456

14571457
// Must construct stuff by the book
14581458
for (; !range.empty; range.popFront())
1459-
emplaceRef(range.front, filler);
1459+
emplaceRef!T(range.front, filler);
14601460
}
14611461
else
14621462
// Doesn't matter whether fill is initialized or not

std/array.d

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ if (isIterable!Range && !isNarrowString!Range && !isInfinite!Range)
5151
size_t i;
5252
foreach (e; r)
5353
{
54-
emplaceRef(result[i], e);
54+
emplaceRef!E(result[i], e);
5555
++i;
5656
}
5757
return cast(E[])result;
@@ -110,6 +110,12 @@ unittest
110110
static assert(bug12315[0].i == 123456789);
111111
}
112112

113+
unittest
114+
{
115+
static struct S{int* p;}
116+
auto a = array(immutable(S).init.repeat(5));
117+
}
118+
113119
/**
114120
Convert a narrow string to an array type that fully supports random access.
115121
This is handled as a special case and always returns a $(D dchar[]),
@@ -1059,13 +1065,13 @@ void insertInPlace(T, U...)(ref T[] array, size_t pos, U stuff)
10591065
{
10601066
static if (is(E : T)) //ditto
10611067
{
1062-
emplaceRef(tmp[j++], stuff[i]);
1068+
emplaceRef!T(tmp[j++], stuff[i]);
10631069
}
10641070
else
10651071
{
10661072
foreach (v; stuff[i])
10671073
{
1068-
emplaceRef(tmp[j++], v);
1074+
emplaceRef!T(tmp[j++], v);
10691075
}
10701076
}
10711077
}
@@ -2434,12 +2440,7 @@ struct Appender(A : T[], T)
24342440
auto bigDataFun() @trusted nothrow { return _data.arr.ptr[0 .. len + 1];}
24352441
auto bigData = bigDataFun();
24362442

2437-
static if (is(Unqual!T == T))
2438-
alias uitem = item;
2439-
else
2440-
auto ref uitem() @trusted nothrow @property { return cast(Unqual!T)item;}
2441-
2442-
emplaceRef(bigData[len], uitem);
2443+
emplaceRef!T(bigData[len], item);
24432444

24442445
//We do this at the end, in case of exceptions
24452446
_data.arr = bigData;
@@ -2484,28 +2485,18 @@ struct Appender(A : T[], T)
24842485
auto bigDataFun() @trusted nothrow { return _data.arr.ptr[0 .. newlen];}
24852486
auto bigData = bigDataFun();
24862487

2487-
enum mustEmplace = is(typeof(bigData[0].opAssign(cast(Unqual!T)items.front))) ||
2488-
!is(typeof(bigData[0] = cast(Unqual!T)items.front));
2488+
alias UT = Unqual!T;
24892489

2490-
static if (is(typeof(_data.arr[] = items[])) && !mustEmplace)
2490+
static if (is(typeof(_data.arr[] = items[])) &&
2491+
!hasElaborateAssign!(Unqual!T) && isAssignable!(UT, ElementEncodingType!Range))
24912492
{
2492-
//pragma(msg, T.stringof); pragma(msg, Range.stringof);
24932493
bigData[len .. newlen] = items[];
24942494
}
2495-
else static if (is(Unqual!T == ElementType!Range))
2496-
{
2497-
foreach (ref it ; bigData[len .. newlen])
2498-
{
2499-
emplaceRef(it, items.front);
2500-
items.popFront();
2501-
}
2502-
}
25032495
else
25042496
{
2505-
static auto ref getUItem(U)(U item) @trusted {return cast(Unqual!T)item;}
25062497
foreach (ref it ; bigData[len .. newlen])
25072498
{
2508-
emplaceRef(it, getUItem(items.front));
2499+
emplaceRef!T(it, items.front);
25092500
items.popFront();
25102501
}
25112502
}

0 commit comments

Comments
 (0)