The lifetime of reduce's internal seed

monarch_dodra via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Apr 22 11:47:19 PDT 2014


On Tuesday, 22 April 2014 at 18:34:47 UTC, Steven Schveighoffer 
wrote:
> On Tue, 22 Apr 2014 14:17:57 -0400, Ali Çehreli 
> <acehreli at yahoo.com> wrote:
>
>> I don't think there is slicing an rvalue though. (?) reduce() 
>> is taking a copy of the seed and then returning a slice to it 
>> because the user slices it in their lambda. It effectively 
>> does the following, which unfortunately compiles:
>>
>> int[] foo()
>> {
>>     int[1] sum;
>>     return sum[];    // <-- no warning
>> }
>
> It's not slicing an rvalue, but the above is trivially no 
> different than:

In this case no, but;
//----
int[1] foo();
int[] a = foo();
//----
*is* slicing an rvalue, and it *does* compile. I don't think 
there needs to be escape analysis to catch this.


More information about the Digitalmars-d-learn mailing list