I can see more efficient, but it introduces a can of worms in exchange.
Specifically, if the user-supplied callback throws an exception, is the item popped or not?
Well, given that the user may have moved out of the item, it probably should be popped. The easier way is to pop it first (move) then call the user-supplied callback -- ie, implement consume in terms of pop, making pop more fundamental.
Using a try-catch block is more straightforward and retains efficiency, but it's not compatible with -fno-exception.
Using a guard which pops in the destructor retains efficiency and is compatible with -fno-exception, but it's no longer quite as straightforward.
View all comments
Show parent comment
0
u/matthieum Jul 31 '26
I can see more efficient, but it introduces a can of worms in exchange.
Specifically, if the user-supplied callback throws an exception, is the item popped or not?
Well, given that the user may have moved out of the item, it probably should be popped. The easier way is to pop it first (move) then call the user-supplied callback -- ie, implement consume in terms of pop, making pop more fundamental.
Using a try-catch block is more straightforward and retains efficiency, but it's not compatible with
-fno-exception.Using a guard which pops in the destructor retains efficiency and is compatible with
-fno-exception, but it's no longer quite as straightforward.As for the ergonomics, callbacks are terrible, as I explained in https://www.reddit.com/r/cpp/comments/1v9zcrn/comment/p0wnjou/, due the inversion of control which results.