Giter Site home page Giter Site logo

functiontype's Introduction

FunctionType

C++ Metaprogramming Utility

A simple single-file implementation of a metaprogramming utility for function type deduction. FunctionType is a template utility struct implemented using explicit specialization. ISO C++14, ISO C++17 and ISO C++20 compatible.
This is a single-include library. No additional dependencies are required.

Characteristics

FunctionType is able to deduce the decayed function's type, arguments and return value for most function types (including lambdas). FunctionType supports the following qualifiers, and their permitted permutations:

  • const qualifier
  • volatile qualifier
  • mutable specifier
  • reference qualifier
  • rvalue reference qualifier
  • noexcept qualifier (since ISO C++17)

Due to the changes to type deduction in ISO C++17, mainly due to the P0012R1 Exception specification as part of the type system feature, ISO C++17 is the current default minimum supported version. If you would like to use the utility with ISO C++14, define FUNCTION_TYPE_CPP14 before you include the FunctionType.h header. This workaround is needed due to MSC not initializing __cplusplus variable properly.

Installation

Copy the FunctionType.h header to your project source files, or include directly from cloned path.

Usage

// Usage:
// FunctionType<[function type]>

// Example:
#include "FunctionType.h"
#include <functional>
int StaticFunction(int, float) { return 1; }
int main()
{
  FunctionType<decltype(&StaticFunction)>::ReturnType var = 0;
  std::function<FunctionType<decltype(&StaticFunction)>::Type> f = StaticFunction;
  return 0;
}

Tests.cpp has the full test suite, please refer to it for additional examples.

Limitations

The following language limitations apply:

  • Cannot be used with C-style variadic functions, as we cannot deduce the function argument types from ellipsis.
  • Cannot be used with generic lambdas, as we cannot deduce the auto type at declaration.
  • Cannot be used with class function overloads, as we cannot deduce the function overload from name alone (Note: it is possible to deduce the overload via a static cast to exact type).

Compatibility

Tested with:

  • g++ 7.4.0
  • clang 6.0.0
  • MSC 1920

Compiled with ISO C++14, ISO C++17, and ISO C++20 flags.

License

The MIT License (MIT)
Copyright (c) 2023 Konstantin Udovickij

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

functiontype's People

Contributors

kudovickij avatar

Stargazers

Elie Wiese-Namir avatar Andrew Gresyk avatar

Watchers

 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.