Skip to main content
19 events
when toggle format what by license comment
Feb 26, 2023 at 15:55 history edited Davislor CC BY-SA 4.0
added 145 characters in body
Feb 26, 2023 at 15:45 comment added Davislor @G.Sliepen Okay, fine, I addressed the case of a generic transformation function on containers.
Feb 26, 2023 at 15:45 history edited Davislor CC BY-SA 4.0
added 159 characters in body
Feb 26, 2023 at 14:12 comment added Davislor @G.Sliepen I think (but it’s one of the things I haven’t tested) that this implementation of traverse should work on views, since std::begin and std::end work on them. However, I’m not convinced that a transformation function with different input and return types will work on a container with the default allocator as a template parameter.
Feb 26, 2023 at 14:09 comment added Davislor @G.Sliepen The original was an answer, not code submitted for review—but maybe people want to review it? It’s true; I haven’t tested it extensively.
Feb 26, 2023 at 14:08 comment added G. Sliepen You might want to post your reworked code as a new Code Review question again. I would add more test cases though.
Feb 26, 2023 at 14:07 history edited Davislor CC BY-SA 4.0
added 98 characters in body
Feb 26, 2023 at 14:06 comment added G. Sliepen You are not wrong if the function you pass in has a concrete type signature, but what if you pass in a lambda with an auto parameter? In @JimmyHu's earlier questions about recursive algorithms, this was one of the issues that was found. There really might be situations where an explicit recursion level is desired. Of course, having recursion terminate when the transformation function can be applied is also a valid use case. Using std::ranges::transform_view() is interesting, and now that makes me wonder if there might be a use case for a recursive_transform_view()...
Feb 26, 2023 at 13:59 comment added Davislor @G.Sliepen I went ahead and implemented that, which simplifies the template considerably.
Feb 26, 2023 at 13:51 history edited Davislor CC BY-SA 4.0
deleted 1761 characters in body
Feb 26, 2023 at 13:44 history edited Davislor CC BY-SA 4.0
deleted 1761 characters in body
Feb 26, 2023 at 12:19 comment added Davislor @G.Sliepen I think a more general solution would be to use std::ranges::views::transform or std::ranges::transform_view to generate an iterator pair, which then gets used to construct the return object.
Feb 26, 2023 at 12:15 comment added Davislor @G.Sliepen This implementation could indeed break onstd::string. That’s because std::string has no .emplace_back, nor does the fallback I wrote work correctly because it cannot .resize. I believe I mentioned this in a comment to my original answer, but you’re correct that I didn’t cover that case. Of course, std::inserter breaks on different STL containers.
Feb 26, 2023 at 12:05 comment added Davislor @G.Sliepen That example might be a good reason for me to re-word my justification, but I don’t think it breaks my code. If you pass in a R f(Container<T>) and a Container<Container<T>>, only the recursive template matches, and it calls traverse<Container<T>, F>. That recursive call matches the base case, because f is invocable on Container<T>. This does not attempt to call traverse recursively, nor would traverse<U, F> match the requires clauses. It just invokes f on its Container<T> arguments and passes the result back up to the caller, which adds it to a Container<R>.
Feb 26, 2023 at 8:21 comment added G. Sliepen “There are only two cases: container, and an object in the domain of the transformation function.“ This is not true. You might have a transformation function that works on containers. For example, maybe you want to replace each innermost container with the sum of its values. And consider that std::string is a container.
Feb 26, 2023 at 5:33 history edited Davislor CC BY-SA 4.0
added 192 characters in body
Feb 26, 2023 at 4:41 history edited Davislor CC BY-SA 4.0
added 697 characters in body
Feb 26, 2023 at 4:22 history edited Davislor CC BY-SA 4.0
added 121 characters in body
Feb 26, 2023 at 4:15 history answered Davislor CC BY-SA 4.0