libflow icon indicating copy to clipboard operation
libflow copied to clipboard

`flatten`: compile error with copy assignment

Open Guekka opened this issue 3 years ago • 5 comments
trafficstars

First of all, I know this library is not supposed to be ready for public usage. But I really love the concept, so I had to try it

Of course, I will understand if you do not want to provide support

I am encountering a compile error that I haven't been able to solve. It is easily reproducible. Godbolt link

Guekka avatar Jan 13 '22 20:01 Guekka

Well, I've just discovered lambdas and functions are not copy assignable. Wrapping the function in a shared_ptr inside the map_adapter makes it compile. Another solution would be to wrap the function in maybe I can make a PR for one of these solutions, if you are interested

Guekka avatar Jan 14 '22 08:01 Guekka

Actually, the best solution is probably to avoid operator= altogether.

std::optional has emplace. If we add emplace to maybe, we can then use it in place of operator=, and this should fix the issue

Guekka avatar Jan 14 '22 08:01 Guekka

No problem filing bug reports! :) Just no promises that I'll be able to fix them...

This is a known (to me) issue that I've run into myself from time to time. If I recall correctly, the reason I used assignment rather than emplace() is that emplace() is non-constexpr in C++17, and cannot be made constexpr because it involves changing the active member of a union (which wasn't allowed until C++20).

At the time I decided that being able to use flatten at compile-time was more important than occasional compile errors when using lambdas, but in hindsight that was probably the wrong trade-off.

tcbrindle avatar Jan 15 '22 17:01 tcbrindle

constexpr support is nice too. So it's either constexpr with a heap allocation, or no constexpr and no heap.

I think we should use std::unique_ptr, as a single heap allocation will probably have no significant impact.

If you tell me what you prefer, I can implement it for you

Guekka avatar Jan 15 '22 19:01 Guekka

I'll need to think about this one -- I don't think allocation should be necessary if we're willing to forego constexpr flatten in C++17 mode, but it would mean adding emplace() to flow::maybe, which it currently doesn't have. There may be other complications as well, I looked it at once before but it's a long time ago and my memory is failing me :).

Leave it with me and I'll see what I can come up with.

tcbrindle avatar Jan 15 '22 23:01 tcbrindle