Code Monkey home page Code Monkey logo

tmppy's Introduction

Build Status

TMPPy is a subset of Python that can be compiled to C++ meta-functions using the py2tmp compiler. This project is aimed at C++ library developers whose libraries include a non-trivial amount of C++ metaprogramming.

Compared to writing C++ metaprogramming code directly, using TMPPy allows that code to be expressed in a more concise and readable way, provides static type checking (avoiding some classes of instantiation-time errors) and produces optimized C++ meta-functions, reducing the compile time for the C++ compilation.

Example

As an example, let's write a metafunction (aka type trait class) add_pointer_multiple such that:

  • add_pointer_multiple<T, 0>::type is T
  • add_pointer_multiple<T, 1>::type is T*
  • add_pointer_multiple<T, 2>::type is T**
  • (and so on)

This can be written as a template, as follows:

template <typename T, int64_t n>
struct add_pointer_multiple {
    using type = typename add_pointer_multiple<T, n - 1>::type*;
};

template <typename T>
struct add_pointer_multiple<T, 0> {
    using type = T;
};

However this syntax is quite verbose and not very readable. For more complex metafunctions this becomes a significant issue, leading to more bugs and more effort when debugging or maintaining the code.

Some C++ metaprogramming libraries (notably Boost's MPL library) can be used to reduce the verbosity, however that comes at the price of slower compile times.

Using TMPPy, the above can be written as:

def add_pointer_multiple(t: Type, n: int) -> Type:
    if n == 0:
        return t
    else:
        return add_pointer_multiple(Type.pointer(t), n-1)

And this TMPPy code can then be compiled to C++ code equivalent to the metafunction above (without the overhead of e.g. MPL).

For more information on TMPPy, see the wiki.

License

TMPPy is released under the Apache 2.0 license. See the LICENSE file for details.

This is not an official Google product.

tmppy's People

Contributors

poletti-marco avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

tmppy's Issues

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.