On Monday, 22 June 2020 at 20:58:58 UTC, Ali Çehreli wrote:

Others have other explanations for this but my understanding is about exception safety: If it changed internal state and returned the front object, you would no be able to make a function like popFront() strongly exception safe. (There are ample documentation for this topic in C++ circles.)

That's one consideration. But, it will be more interesting in knowing (based on actual usage):

(a) how many bugs % are due to exception un-safe
(b) how many bugs % are due to front / popFront separation?

And which is more bug-prone for a typical programmer? my gut feeling is (b), at least we just saw one in this thread.


And

-- loop thru a static structure content like a simple array, why we need to worry about exception safe?

-- loop thru dynamically generated content, esp. network or more external complex structure may worth considering exception safety. But even there, do people always need to call !range.empty() check first? when it's not empty, how much more exception un-safety that popAndReturnFront() can introduce than the combination of `front(); ...; popFront();"?

And why not provide a popAndReturnFront(), and let the user decide based on his/her own actual usage?

Another reason is cohesion: We want functions to have as little responsibility as possible (ideally single).

Again we have to weight which way is more bug-prone, any actual statistics on the above (a) v.s (b)?


> (or do we have another function for this)?

There are many useful functions in std.range:

  https://dlang.org/phobos/std_range.html

The "take" and "drop" functions may be useful.

Use these functions inside a while(!range.empty()) {...} can only introduce more code complexity.

Reply via email to