| 123456789101112131415161718192021222324252627282930313233 |
- //
- // capture.h
- // iterator
- //
- // Created by Sam Jaffe on 9/23/25.
- // Copyright © 2025 Sam Jaffe. All rights reserved.
- //
- #pragma once
- #include <functional>
- namespace iterator::detail {
- template <typename P, typename T> class CaptureFn {
- private:
- using R = std::invoke_result_t<P, T>;
- private:
- P fn_;
- void const * key_;
- mutable R cache_;
- public:
- CaptureFn(P fn) : fn_(fn) {} // NOLINT
- auto const & operator()(T & arg) const {
- if (key_ != &arg) {
- new (&cache_) R(std::invoke(fn_, std::forward<T>(arg)));
- }
- return cache_;
- }
- };
- }
|