On Friday, 10 January 2014 at 19:40:35 UTC, Manu wrote:
On 11 January 2014 00:31, John Colvin
On Friday, 10 January 2014 at 14:28:09 UTC, John Colvin wrote:
or if you want something short and simple, define a free function:
auto popFrontRet(R)(ref R range)
    if(isInputRange!R)
{
    range.popFront();
    assert(!range.empty);
    return range.front;
}


This is what I've done. I'm just surprised that such an obvious function
doesn't exist, and suspect I was just retarded at phobos again.
Having a function that does this is kinda important to simplify lots of expressions that otherwise need to be broken out across a bunch of lines.

Does nobody see this coming up in their code? I have it basically every time I use ranges, and as usual, surprised others don't feel the same way.

Could you confirm that what you wanted is:
"pop, then take the new front" [1]
and not
"pop and get the popped element" [2]
?

In case of [1], how do you know if you can call the function, as knowing not empty is not enough? Take a range with a single element: First you pop, then it's empty, then you take front (boom).

In case of [2], honestly, I've never felt the need for that. As I said previously, I think it's better to pop *once* you are done with the object, rather than once you start using it. It's better in terms of exception safety, and in certain cases, a premature pop can invalidate the element you want to operate on (byLine) :/

Reply via email to