# fu2::function an improved drop-in replacement to std::function
![](https://img.shields.io/badge/Version-4.0.0-0091EA.svg) ![](https://img.shields.io/badge/License-Boost-blue.svg) [![Build Status](https://travis-ci.org/Naios/function2.svg?branch=master)](https://travis-ci.org/Naios/function2) [![Build status](https://ci.appveyor.com/api/projects/status/1tl0vqpg8ndccats/branch/master?svg=true)](https://ci.appveyor.com/project/Naios/function2/branch/master)
Provides improved implementations of `std::function`:
- **copyable** `fu2::function`
- **move-only** `fu2::unique_function` (capable of holding move only types)
- **non-owning** `fu2::function_view` (capable of referencing callables in a non owning way)
that provide many benefits and improvements over `std::function`:
- [x] **const**, **volatile**, **reference** and **noexcept** correct (qualifiers are part of the `operator()` signature)
- [x] **convertible** to and from `std::function` as well as other callable types
- [x] **adaptable** through `fu2::function_base` (internal capacity, copyable and exception guarantees)
- [x] **overloadable** with an arbitrary count of signatures (`fu2::function<bool(int), bool(float)>`)
- [x] **full allocator support** in contrast to `std::function`, which doesn't provide support anymore
- [x] **covered** by many unit tests and continuous integration services (*GCC*, *Clang* and *MSVC*)
- [x] **header only**, just copy and include `function.hpp` in your project
- [x] **permissively licensed** under the **boost** license
## Table of Contents
* **[Documentation](#documentation)**
* **[How to use](#how-to-use)**
* **[Constructing a function](#constructing-a-function)**
* **[Non copyable unique functions](#non-copyable-unique-functions)**
* **[Convertibility of functions](#convertibility-of-functions)**
* **[Adapt function2](#adapt-function2)**
* **[Performance and optimization](#performance-and-optimization)**
* **[Small functor optimization](#small-functor-optimization)**
* **[Compiler optimization](#compiler-optimization)**
* **[std::function vs fu2::function](#stdfunction-vs-fu2function)**
* **[Coverage and runtime checks](#coverage-and-runtime-checks)**
* **[Compatibility](#compatibility)**
* **[License](#licence)**
* **[Similar implementations](#similar-implementations)**
## Documentation
### How to use
**function2** is implemented in one header (`function.hpp`), no compilation is required.
Just copy the `function.hpp` header in your project and include it to start.
It's recommended to import the library as git submodule using CMake:
```sh
# Shell:
git submodule add https://github.com/Naios/function2.git
```
```cmake
# CMake file:
add_subdirectory(function2)
# function2 provides an interface target which makes it's
# headers available to all projects using function2
target_link_libraries(my_project function2)
```
Use `fu2::function` as a wrapper for copyable function wrappers and `fu2::unique_function` for move only types.
The standard implementation `std::function` and `fu2::function` are convertible to each other, see [the chapter convertibility of functions](#convertibility-of-functions) for details.
A function wrapper is declared as following:
```c++
fu2::function<void(int, float) const>
// Return type ~^ ^ ^ ^
// Parameters ~~~~~|~~~~~| ^
// Qualifier ~~~~~~~~~~~~~~~~~~~|
```
* **Return type**: The return type of the function to wrap.
* **Arguments**: The argument types of the function to wrap.
Any argument types are allowed.
* **Qualifiers**: There are several qualifiers allowed:
- **no qualifier** provides `ReturnType operator() (Args...)`
- Can be assigned from const and no const objects (*mutable lambdas* for example).
- **const** provides `ReturnType operator() (Args...) const`
- Requires that the assigned functor is const callable (won't work with *mutable lambdas*),
- **volatile** provides `ReturnType operator() (Args...) volatile`
- Can only be assigned from volatile qualified functors.
- **const volatile** provides `ReturnType operator() (Args...) const volatile`
- Same as const and volatile together.
- **r-value (one-shot) functions** `ReturnType operator() (Args...) &&`
- one-shot functions which are invalidated after the first call (can be mixed with `const`, `volatile` and `noexcept`). Can only wrap callable objects which call operator is also qualified as `&&` (r-value callable). Normal (*C*) functions are considered to be r-value callable by default.
- **noexcept functions** `ReturnType operator() (Args...) noexcept`
- such functions are guaranteed not to throw an exception (can be mixed with `const`, `volatile` and `&&`). Can only wrap functions or callable objects which call operator is also qualified as `noexcept`. Requires enabled C++17 compilation to work (support is detected automatically). Empty function calls to such a wrapped function will lead to a call to `std::abort` regardless the wrapper is configured to support exceptions or not (see [adapt function2](#adapt-function2)).
* **Multiple overloads**: The library is capable of providing multiple overloads:
```cpp
fu2::function<int(std::vector<int> const&),
int(std::set<int> const&) const> fn = [] (auto const& container) {
return container.size());
};
```
### Constructing a function
`fu2::function` and `fu2::unique_function` (non copyable) are easy to use:
```c++
fu2::function<void() const> fun = [] {
// ...
};
// fun provides void operator()() const now
fun();
```
### Non copyable unique functions
`fu2::unique_function` also works with non copyable functors/ lambdas.
```c++
fu2::unique_function<bool() const> fun = [ptr = std::make_unique<bool>(true)] {
return *ptr;
};
// unique functions are move only
fu2::unique_function<bool() const> otherfun = std::move(fun):
otherfun();
```
### Non owning functions
A `fu2::function_view` can be used to create a non owning view on a persistent object. Note that the view is only valid as long as the object lives.
```c++
auto callable = [ptr = std::make_unique<bool>(true)] {
return *ptr;
};
fu2::function_view<bool() const> view(callable);
```
### Convertibility of functions
`fu2::function`, `fu2::unique_function` and `std::function` are convertible to each other when:
- The return type and parameter type match.
- The functions are both volatile or not.
- The functions are const correct:
- `noconst = const`
- `const = const`
- `noconst = noconst`
- The functions are copyable correct when:
- `unique = unique`
- `unique = copyable`
- `copyable = copyable`
- The functions are reference correct when:
- `lvalue = lvalue`
- `lvalue = rvalue`
- `rvalue = rvalue`
- The functions are `noexcept` correct when:
- `callable = callable`
- `callable = noexcept callable `
- `noexcept callable = noexcept callable`
| Convertibility from \ to | fu2::function | fu2::unique_function | std::function |
| ------------------------ | ------------- | -------------------- | ------------- |
| fu2::function | Yes | Yes | Yes |
| fu2::unique_function | No | Yes | No |
| std::function | Yes | Yes | Yes |
```c++
fu2::function<void()> fun = []{};
// OK
std::function<void()> std_fun = fun;
// OK
fu2::unique_function<void()> un_fun = fun;
// Error (non copyable -> copyable)
fun = un_fun;
// Error (non copyable -> copyable)
fun = un_fun;
```
### Adapt function2
function2 is adaptable through `fu2::function_base` which allows you to set:
- **IsOwning**: defines whether the function owns its contained object
- **Copyable:** defines if the function is copyable or not.
- **Capacity:** defines the internal capacity used for [sfo optimization](#small-functor-optimization):
```cpp
struct my_capacity {
static constexpr std::size_t capacity = sizeof(my_type);
没有合适的资源?快使用搜索试试~ 我知道了~
stdfunction的改进和可配置的直接替换,支持仅移动类型、多重重载等___下载.zip
共43个文件
cpp:15个
txt:6个
cmake:5个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 78 浏览量
2023-04-18
00:13:12
上传
评论
收藏 45KB ZIP 举报
温馨提示
stdfunction的改进和可配置的直接替换,支持仅移动类型、多重重载等___下载.zip
资源推荐
资源详情
资源评论
收起资源包目录
stdfunction的改进和可配置的直接替换,支持仅移动类型、多重重载等___下载.zip (43个子文件)
function2-master
.editorconfig 184B
include
function2
function2.hpp 71KB
CMakeLists.txt 4KB
tools
travis-ci.sh 2KB
.travis.yml 2KB
conanfile.py 858B
.github
CODEOWNERS 9B
CONTRIBUTING.md 378B
ISSUE_TEMPLATE.md 588B
PULL_REQUEST_TEMPLATE.md 466B
.gitattributes 427B
.clang-format 235B
LICENSE.txt 1KB
Findfunction2.cmake 420B
cmake
CMakeLists.txt 40B
configure_compiler.cmake 465B
config.cmake.in 171B
compiler
gcc.cmake 231B
msvc.cmake 891B
clang.cmake 250B
.gitmodules 102B
test
view-test.cpp 4KB
CMakeLists.txt 2KB
standard-compliant-test.cpp 2KB
assign-and-constructible-test.cpp 6KB
self-containing-test.cpp 444B
regressions.cpp 6KB
googletest
dist-subproject
CMakeLists.txt 270B
main.cpp 122B
noexcept-test.cpp 2KB
empty-function-call-test.cpp 3KB
multi-signature-test.cpp 1KB
overload-test.cpp 968B
function2-test.hpp 11KB
dist-package
CMakeLists.txt 238B
main.cpp 122B
functionality-test.cpp 5KB
type-test.cpp 3KB
playground.cpp 322B
build-test.cpp 540B
.gitignore 465B
appveyor.yml 1KB
Readme.md 11KB
共 43 条
- 1
资源评论
快撑死的鱼
- 粉丝: 1w+
- 资源: 9154
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功