1 Star 0 Fork 1

paul3rd / function_ref

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
BSD-2-Clause

nontype<functional>

GitHub tag GitHub license CMake

Provide complete implementation of std::function, std::function_ref, and std::move_only_function equivalent to those in the C++23 <functional> header.

Highlights

  • Macro-free implementation
  • The size of each specialization is two pointers
  • Not require RTTI
  • Support classes without operator()

The implementation does not guarantee the best performance under all use cases but provides adequate code size & quality while maintaining full conformance.[^1]

Supported toolchains

Toolset Standard Library Test Environment
GCC >= 11.1.0 libstdc++ Ubuntu 20.04
MSVC >= 14.30 Microsoft STL Visual Studio 2022

Installation

It's a header-only library. You may also install and consume its CMake targets:

find_package(nontype_functional CONFIG REQUIRED)
target_link_libraries("main" PRIVATE std23::nontype_functional)

Getting started

#include <std23/function_ref.h>

using std23::function_ref;

void parse_ini(function_ref<size_t(char *, size_t)> read_cb);

...

#include <stdio.h>

int main()
{
    auto fp = ::fopen("my.ini", "r");
    parse_ini([fp](auto ptr, auto n)
              { return ::fread(ptr, 1, n, fp); });
    ::fclose(fp);
}

Ignore the fact that the code has no error handling or resource-safety; the callable wrappers, function_ref in the example, generalized the idea of callbacks. You can pass anything with a matching call pattern to another function, parse_ini in here, without turning the latter into a template.

Now, what if you have an existing class that can read data, but it's not a function object?

class data_source
{
    ...

  public:
    auto read(char *, size_t) -> size_t;
};

Then you may designate a named member function, read in this example, to serve the role of an operator():

using std23::nontype;

int main()
{
    data_source input;
    parse_ini({nontype<&data_source::read>, input});
}

The nontype tag generalized the idea of delegates from other languages, like C♯. What replaces operator() doesn't have to be a member function. You can also use a free function or even a lambda:

int main()
{
    auto fp = ::fopen("my.ini", "r");
    parse_ini({nontype<[](FILE *fh, auto ptr, auto n)
                       { return ::fread(ptr, 1, n, fh); }>,
               fp});
    ::fclose(fp);
}

Feels like creating a member function for FILE on the fly, isn't it?

Roadmap

  • 0.8 – std::function_ref & std::function
  • 0.9 – std::move_only_function
  • 1.0 – nontype_t constructors for move_only_function
  • 1.1 – copyable_function from P2548
  • 1.2 – Support C++20 modules

See also

cppreference page for std::function
cppreference page for std::move_only_function
std::function_ref specification

[^1]: Except for std::function's target() member function, which is unimplemented because it requires RTTI.

BSD 2-Clause License Copyright (c) 2022, Zhihao Yuan All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

简介

function_ref: a type-erased callable reference 展开 收起
C++ 等 2 种语言
BSD-2-Clause
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/paul3rd/function_ref.git
git@gitee.com:paul3rd/function_ref.git
paul3rd
function_ref
function_ref
main

搜索帮助

344bd9b3 5694891 D2dac590 5694891