https://issues.dlang.org/show_bug.cgi?id=12625
--- Comment #9 from Steven Schveighoffer <[email protected]> --- (In reply to Sobirari Muhomori from comment #8) > tempCString is another example of an rvalue reference. Works good so far. Another good example of an error waiting to happen: char *str = someString.tempCString; This would be more difficult for the compiler to figure out, because the address-taking is done inside a method. tempCString also has a destructor, so pushing the destruction of the temporary to after the expression is actually easier to mandate/do. > > > example requires blind faith that the receiver of the slice never does > > anything bad with it, and assumes the compiler doesn't cleverly reclaim the > > memory for the rvalue seeing as it wasn't used anywhere. > > If the temporary isn't used, it should be reclaimed. What are the rules for temporaries that aren't further used in the expression? In this case, the result of foo isn't passed into another function, just a slice is. I suppose if the rules mandate any temporaries live until after the expression is completed, then we don't need to worry about the memory being reclaimed mid expression. We are then just left with the issue of assigning a pointer that points at the temporary. How to figure out generally that this is the case? Not sure. IMO, however, the original problem (assigning a slice of a temporary) should be easy to flag, since the compiler understands what is happening, and knows that it is obviously invalid. --
