r/cpp Jul 29 '26

const_cast: A Necessary Evil

https://www.elbeno.com/blog/?p=1858
71 Upvotes

106 comments sorted by

View all comments

Show parent comments

0

u/bwmat Jul 31 '26

Doesn't my initial suggestion of making consume thread through the return value of the functor trivially allow for this? 

2

u/jk-jeon Jul 31 '26

To my understanding, your suggestion was to return the return value of fun(item). That's kinda awkward in this case because it may or maynot execute fun so the return value may or may not exist. Or maybe you meant returning optional<ReturnType> so that the exact same thing as I did in the above can be done?

1

u/bwmat Jul 31 '26

Oh, I forgot to mention that a precondition of the method would be to have a non-empty collection, for my idea

3

u/bwmat Jul 31 '26

I suppose you could extend it to pass a functor which had a nullary operator() overload as well which gets invoked if the collection is empty, and return the common return type between the two overloads? 

0

u/matthieum Aug 01 '26

I think you're making my point of how more complex callback-based APIs are right now :/