GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Categories
  • Newsletter
  • Submit
  • Login
  • Awesome C++
  • Containers

This view

  • Original page
  • New address

GeezerVison is showing cpp.libhunt.com — function2 Alternatives - C++ Containers | LibHunt

  • Subscribe
  • Submit
  • Categories
  • About
  • Login

function2

Improved and configurable drop-in replacement to std::function that supports move only types, multiple overloads and more

Naios logo
naios.github.io    Source Code    Docs    Changelog
Suggest Changes
Take the C++ Developer Survey - About 6 minutes on the tools, libraries and AI you use with C++. You get the results by email. Take the survey »
Popularity
4.1
Stable
Activity
3.3
-
Stars 607
Watchers 13
Forks 56
Last Commit over 1 year ago

Description

Provides two improved implementations of std::function:

* copyable fu2::function * move-only fu2::unique_function (capable of holding move only types)

that provide many benefits and improvements over std::function:

* const, volatile and reference correct (qualifiers are part of the operator() signature). * convertible to and from std::function as well as other callable types. * adaptable through fu2::function_base (internal capacity, copyable and exception guarantees) * overloadable with an arbitrary count of signatures (fu2::function) * full allocator support in contrast of std::function which doesn’t provide support anymore * covered by unit tests and continuous integration (GCC, Clang and MSVC). * header only, just copy and include function.hpp in your project, permissive licensed under boost.

Programming language: C++
License: gtkbook License
Tags: Containers     Portable     Utilities     C++14     Type Erasure    
Latest version: v4.2.3

function2 alternatives and similar libraries

Based on the "Containers" category.
Alternatively, view function2 alternatives based on common mentions on social networks and blogs.

  • pybind11

    9.5 9.1 function2 VS pybind11
    Seamless operability between C++11 and Python
    pybind logo
  • PEGTL

    6.6 9.4 function2 VS PEGTL
    Parsing Expression Grammar Template Library
    taocpp logo
  • SaaSHub - Software Alternatives and Reviews

    SaaSHub helps you find the best software and product alternatives
    Promo www.saashub.com
    SaaSHub Logo
  • FunctionalPlus

    6.6 5.5 L4 function2 VS FunctionalPlus
    Functional Programming Library for C++. Write concise and readable C++ code.
    Dobiasd logo
  • sparsehash

    6.2 0.0 L1 function2 VS sparsehash
    C++ associative containers
    sparsehash logo
  • sparsepp

    5.7 3.3 function2 VS sparsepp
    A fast, memory efficient hash map for C++
    greg7mdp logo
  • Hopscotch map

    4.8 3.7 L4 function2 VS Hopscotch map
    C++ implementation of a fast hash map and hash set using hopscotch hashing
    Tessil logo
  • LSHBOX

    3.8 0.0 L2 function2 VS LSHBOX
    A c++ toolbox of locality-sensitive hashing (LSH), provides several popular LSH algorithms, also support python and matlab.
    RSIA-LIESMARS-WHU logo
  • sparsehash-c11

    3.7 0.0 L3 function2 VS sparsehash-c11
    Experimental C++11 version of sparsehash
    sparsehash logo
  • Hashmaps

    3.7 0.0 L1 function2 VS Hashmaps
    Various open addressing hashmap algorithms in C++
    goossaert logo
  • semver

    3.6 6.9 function2 VS semver
    Semantic Versioning for modern C++
    Neargye logo
  • stx-btree

    3.3 0.0 function2 VS stx-btree
    OBSOLETE, contained in https://github.com/tlx/tlx - STX B+ Tree C++ Template Classes -
    bingmann logo
  • KOMIHASH

    3.3 4.6 function2 VS KOMIHASH
    Very fast, high-quality hash function, discrete-incremental and streamed hashing-capable (non-cryptographic, header-only C/C++) 27GB/s + PRNG
    avaneev logo
  • dynamic_bitset

    2.9 6.2 function2 VS dynamic_bitset
    Simple Useful Libraries: C++17/20 header-only dynamic bitset
    pinam45 logo
  • C++ B-tree

    2.3 0.0 function2 VS C++ B-tree
    DISCONTINUED. Git mirror of the official (mercurial) repository of cpp-btree
    algorithm-ninja logo
  • flat_map

    1.8 7.6 function2 VS flat_map
    Header only associative linear container.
    Flast logo
  • [Boost.]Multi

    1.6 10.0 function2 VS [Boost.]Multi
    Multidimensional arrays for C++. (Not an official Boost library) \\ This is a mirror of gitlab.com/correaa/boost-multi
    correaa logo
  • sqlitemap

    1.3 8.1 function2 VS sqlitemap
    sqlitemap is a lightweight C++ wrapper around SQLite that provides a simple, map-like interface.
    bw-hro logo
  • Optional Argument in C++

    1.2 0.0 function2 VS Optional Argument in C++
    Named Optional Arguments in C++17
    vincent-picaud logo
  • Learn Project

    1.0 0.0 function2 VS Learn Project
    go study
    SAMARITAN2046 logo
  • f-code

    1.0 7.2 function2 VS f-code
    DISCONTINUED. a C++ library consisting of basic functionality [GET https://api.github.com/repos/StanislavPetrov15/f-code: 404 - Not Found // See: https://docs.github.com/rest/repos/repos#get-a-repository]
    StanislavPetrov15 logo

* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.

Do you think we are missing an alternative of function2 or a related project?

Add another 'Containers' Library

Popular Comparisons

  • function2
    vs
    Inja
  • function2
    vs
    FunctionalPlus
  • function2
    vs
    LSHBOX
  • function2
    vs
    Hashmaps
  • function2
    vs
    stx-btree

README

fu2::function an improved drop-in replacement to std::function

Build Status Build status

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
    • How to use
    • Constructing a function
    • Non copyable unique functions
    • Convertibility of functions
    • Adapt function2
  • Performance and optimization
    • Small functor optimization
    • Compiler optimization
    • std::function vs fu2::function
  • Coverage and runtime checks
  • Compatibility
  • License
  • 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:

# Shell:
git submodule add https://github.com/Naios/function2.git
# 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 for details.

A function wrapper is declared as following:

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).
  • 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:

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.

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.

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
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: cpp struct my_capacity { static constexpr std::size_t capacity = sizeof(my_type); static constexpr std::size_t alignment = alignof(my_type); };
  • IsThrowing defines if empty function calls throw an fu2::bad_function_call exception, otherwise std::abort is called.
  • HasStrongExceptGuarantee defines whether the strong exception guarantees shall be met.
  • Signatures: defines the signatures of the function.

The following code defines an owning function with a variadic signature which is copyable and sfo optimization is disabled:

template<typename Signature>
using my_function = fu2::function_base<true, true, fu2::capacity_none, true, false, Signature>;

The following code defines a non copyable function which just takes 1 argument, and has a huge capacity for internal sfo optimization. Also it must be called as r-value.

template<typename Arg>
using my_consumer = fu2::function_base<true, false, fu2::capacity_fixed<100U>,
                                       true, false, void(Arg)&&>;

// Example
my_consumer<int, float> consumer = [](int, float) { }
std::move(consumer)(44, 1.7363f);

Performance and optimization

Small functor optimization

function2 uses small functor optimization like the most common std::function implementations which means it allocates a small internal capacity to evade heap allocation for small functors.

Smart heap allocation moves the inplace allocated functor automatically to the heap to speed up moving between objects.

It's possible to disable small functor optimization through setting the internal capacity to 0.

Coverage and runtime checks

Function2 is checked with unit tests and valgrind (for memory leaks), where the unit tests provide coverage for all possible template parameter assignments.

Compatibility

Tested with:

  • Visual Studio 2017+ Update 3
  • Clang 3.8+
  • GCC 5.4+

Every compiler with modern C++14 support should work. function2 only depends on the standard library.

License

function2 is licensed under the very permissive Boost 1.0 License.

Similar implementations

There are similar implementations of a function wrapper:

  • pmed/fixed_size_function
  • stdex::function - A multi-signature function implementation.
  • multifunction - Example from Boost.TypeErasure, another multi-signature function.
  • std::function - Standard.
  • boost::function - The one from Boost.
  • func::function - From this blog.
  • generic::delegate - Fast delegate in C++11, also see here.
  • ssvu::FastFunc - Another Don Clugston's FastDelegate, as shown here.
  • cxx_function::function - By David Krauss

Also check out the amazing CxxFunctionBenchmark which compares several implementations.


*Note that all licence references and agreements mentioned in the function2 README section above are relevant to that project's source code only.

Do not miss the trending, libraries, news and articles with our weekly report.

Awesome C++ is part of the LibHunt network. Terms. Privacy Policy.

(CC)
BY-SA
Spin The Wheel Of Names for a cryptographically secure random name picker.
Schedule a when to meet poll.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.