GithubHelp home page GithubHelp logo

amir110 / callable.hpp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sth/callable.hpp

1.0 0.0 0.0 10 KB

C++ 11 type traits for callable objects like lambda functions or objects with operator().

License: MIT License

C++ 98.04% Python 1.96%

callable.hpp's Introduction

callable.hpp

A C++ 11 template for type traits of callables (like functions, objects with operator(), lambda functions, ...).

The template, callable_traits, can be used to determine useful properties of callable objects, like return type, number of arguments and types of the individual arguments.

It is especially useful in template functions/... that handle callable objects, for example as callbacks. callable_traits allows the code to treat different types of callables uniformly without wrapping them in a std::function<>.

Features

Callables

Callables are things that can be called with ():

  • Functions
  • Function pointers
  • Objects with operator()
  • std::function<...>
  • Lambda functions

callable_traits can be used to treat all these different kinds of callables uniformly in a template function.

Traits

callable_traits deduces the following properties from the type T of a callable:

  • The return type

      typename callable_traits<T>::return_type
    
  • The number of arguments

      callable_traits<T>::argc
    
  • The individual argument types

      typename callable_traits<T>::template argument_type<N>
    
  • A function type representing the call:

      typename callable_traits<T>::function_type
    

Example

template<typename Callable>
void metainfo(Callable f) {
   std::cout << callable_traits<Callable>::argc << " arguments" << std:endl;
}

Compilation

Library

The library is header only, so it doesn't need to be compiled. Just include the header file when you need it.

Test cases

To compile the test cases, you need:

  • SCons as a build tool
  • Boost.Test for the test cases

With these tools you can compile callable_test by running scons in the main directory:

scons

License

MIT. See LICENSE.txt for more details.

callable.hpp's People

Contributors

sth avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.