beman.cache_latest
beman.cache_latest: A Beman Library
beman.cache_latest is a C++ ranges adaptor that caches the result of the last dereference of the underlying iterator. The reason for doing this is efficiency - specifically avoiding extra iterator dereferences.
The library conforms to The Beman Standard.
Implements: std::views::cache_latest proposed in P3138 views::cache_latest and in the working draft for C++26.
Usage
The following code snippet illustrates using beman::cache_latest:
#include <beman/cache_latest/cache_latest.hpp>
int main()
{
std::vector<int> v = {1, 2, 3, 4, 5};
auto even_squares = v
| std::views::transform([](int i){
std::print("transform: {}\n", i);
return i * i;
})
| beman::cache_latest
| std::views::filter([](int i){
std::print("filter: {}\n", i);
return i % 2 == 0;
});
for (int i : even_squares) {
std::print("Got: {}\n", i);
}
}
Full runnable examples can be found in examples/.
Dependencies
Development
See the Contributing Guidelines.
Integrate beman.cache_latest into your project
Build
You can build cache_latest using a CMake workflow preset:
cmake --workflow --preset gcc-release
To list available workflow presets, you can invoke:
cmake --list-presets=workflow
For details on building beman.cache_latest without using a CMake preset, refer to the Contributing Guidelines.
Installation
Vcpkg
The preferred way to install cache_latest is via vcpkg. To do so, after installing vcpkg itself, you need to add support for the Beman project’s vcpkg registry by configuring a vcpkg-configuration.json file (which cache_latest provides).
Then, simply run vcpkg install beman-cache-latest.
Manual
To install beman.cache_latest globally after building with the gcc-release preset, you can run:
sudo cmake --install build/gcc-release
Alternatively, to install to a prefix, for example /opt/beman, you can run:
sudo cmake --install build/gcc-release --prefix /opt/beman
This will generate the following directory structure:
/opt/beman
├── include
│ └── beman
│ └── cache_latest
│ ├── cache_latest.hpp
│ └── ...
└── lib
└── cmake
└── beman.cache_latest
├── beman.cache_latest-config-version.cmake
├── beman.cache_latest-config.cmake
└── beman.cache_latest-targets.cmake
CMake Configuration
If you installed beman.cache_latest to a prefix, you can specify that prefix to your CMake project using CMAKE_PREFIX_PATH; for example, -DCMAKE_PREFIX_PATH=/opt/beman.
You need to bring in the beman.cache_latest package to define the beman::cache_latest CMake target:
find_package(beman.cache_latest REQUIRED)
You will then need to add beman::cache_latest to the link libraries of any libraries or executables that include beman.cache_latest headers.
target_link_libraries(yourlib PUBLIC beman::cache_latest)
Contributing
Please do! You encourage you to checkout our contributor’s guide. Issues and pull requests are appreciated.