beman.copyable_function: A Beman Library Implementation of copyable_function (P2548)
beman.copyable_function is a type-erased function wrapper that can represent any copyable callable matching
the function signature R(Args...). The library conforms to the The Beman Standard.
Implements: std::copyable_function proposed in P2548R6.
Status: Under development and not yet ready for production use.
beman.copyable_function is licensed under the Apache License v2.0 with LLVM Exceptions.
The following code snippet illustrates copyable_function:
#include <beman/copyable_function/copyable_function.hpp>
// a Callable object
struct Callable {
int operator()() { return 42; }
int operator()() const noexcept { return 43; }
};
int main()
{
beman::copyable_function<int()> f(Callable{});
int x = f();
return 0;
}
Full runnable examples can be found in examples/.
This project requires at least the following to build:
- A C++ compiler that conforms to the C++17 standard or greater
- CMake 3.30 or later
- (Test Only) GoogleTest
You can disable building tests by setting CMake option BEMAN_COPYABLE_FUNCTION_BUILD_TESTS to
OFF when configuring the project.
| Compiler | Version | C++ Standards | Standard Library |
|---|---|---|---|
| GCC | 15-13 | C++26, C++23 | libstdc++ |
| GCC | 12 | C++23 | libstdc++ |
| Clang | 22-19 | C++26, C++23 | libstdc++, libc++ |
| Clang | 18 | C++26, C++23 | libc++ |
| Clang | 18 | C++23 | libstdc++ |
| Clang | 17 | C++26, C++23 | libc++ |
| AppleClang | latest | C++26, C++23 | libc++ |
| MSVC | latest | C++23 | MSVC STL |
See the Contributing Guidelines.
You can build copyable_function using a CMake workflow preset:
cmake --workflow --preset gcc-releaseTo list available workflow presets, you can invoke:
cmake --list-presets=workflowFor details on building beman.copyable_function without using a CMake preset, refer to the Contributing Guidelines.
To install beman.copyable_function globally after building with the gcc-release preset, you can
run:
sudo cmake --install build/gcc-releaseAlternatively, to install to a prefix, for example /opt/beman, you can run:
sudo cmake --install build/gcc-release --prefix /opt/bemanThis will generate the following directory structure:
/opt/beman
├── include
│ └── beman
│ └── copyable_function
│ ├── copyable_function.hpp
│ └── ...
└── lib
└── cmake
└── beman.copyable_function
├── beman.copyable_function-config-version.cmake
├── beman.copyable_function-config.cmake
└── beman.copyable_function-targets.cmakeIf you installed beman.copyable_function 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.copyable_function package to define the beman::copyable_function CMake
target:
find_package(beman.copyable_function REQUIRED)You will then need to add beman::copyable_function to the link libraries of any libraries or
executables that include beman.copyable_function headers.
target_link_libraries(yourlib PUBLIC beman::copyable_function)To use beman.copyable_function in your C++ project,
include an appropriate beman.copyable_function header from your source code.
#include <beman/copyable_function/copyable_function.hpp>Note
beman.copyable_function headers are to be included with the beman/copyable_function/ prefix.
Altering include search paths to spell the include target another way (e.g.
#include <copyable_function.hpp>) is unsupported.
Please do! You encourage you to checkout our contributor's guide. Issues and pull requests are appreciated.