Code Monkey home page Code Monkey logo

monster's Introduction

Monster LICENSE Documentation Language Platform Gitter Chat

Advanced C++ Template MetaProgramming Framework

Overview

#include <cstdint>
#include <monster.hpp>

using namespace monster;

int main(int argc, char* argv[])
{
    // arrange the same elements adjacent in a sequence, keep the relative order
    using a1 = adjacent_t<std::tuple<char, double, char, int, double>>;
    using a2 = adjacent_t<std::index_sequence<4, 3, 0, 3, 2, 4, 5, 3>>;

    static_assert(std::is_same_v<a1, std::tuple<char, char, double, double, int>>);
    static_assert(std::is_same_v<a2, std::index_sequence<4, 4, 3, 3, 3, 0, 2, 5>>);

    // Boyer-Moore-Horspool (BMH) algorithm searches for occurrences of a sequence within another sequence
    using b1 = bmh_t<std::tuple<int, char, int>, std::tuple<int, int, char, int, char, int, char, int>>;
    using b2 = bmh_t<std::integer_sequence<int, 7, 5>, std::integer_sequence<int, 7, 5, 4, 0, 7, 5, 9>>;

    static_assert(std::is_same_v<b1, std::index_sequence<1, 3, 5>>);
    static_assert(std::is_same_v<b2, std::index_sequence<0, 4>>);

    // Knuth–Morris–Pratt (KMP) algorithm searches for occurrences of a sequence within another sequence
    using k1 = kmp_t<std::tuple<int, char, int>, std::tuple<int, int, char, int, char, int, char, int>>;
    using k2 = kmp_t<std::integer_sequence<int, 7, 5>, std::integer_sequence<int, 7, 5, 4, 0, 7, 5, 9>>;

    static_assert(std::is_same_v<k1, std::index_sequence<1, 3, 5>>);
    static_assert(std::is_same_v<k2, std::index_sequence<0, 4>>);

    // find K-th smallest element in a sequence (k == 2)
    using min1 = select_t<2, std::tuple<short, int, double, int, char>>;
    using min2 = select_t<2, std::integer_sequence<int, -2, 1, 0, -7, 4, 3>>;

    static_assert(std::is_same_v<min1, short>);
    static_assert(std::is_same_v<min2, c_<-2>>);

    // find K-th greatest element in a sequence (k == 2)
    using max1 = select_t<2, std::tuple<short, int, double, int, char>, greater_equal_t>;
    constexpr auto max2 = select_v<2, std::integer_sequence<int, -2, 1, 0, -7, 4, 3>, greater_equal_t>;

    static_assert(std::is_same_v<max1, int>);
    static_assert(max2 == 3);

    // returns element at specific index of a sequence
    using e1 = element_t<1, std::tuple<char, double, int>>;
    using e2 = element_t<3, std::integer_sequence<int, 1, -2, 7, 4>>;

    constexpr auto e3 = element_v<3, std::integer_sequence<int, 1, -2, 7, 4>>;

    static_assert(std::is_same_v<e1, double>);
    static_assert(std::is_same_v<e2, c_4>);

    static_assert(e3 == 4);

    // remove duplicate elements from a sequence, keep the first appearance
    using u1 = unique_t<std::tuple<int, char, int, double>>;
    using u2 = unique_t<std::integer_sequence<int, 2, 2, 3, 4, 3>>;

    static_assert(std::is_same_v<u1, std::tuple<int, char, double>>);
    static_assert(std::is_same_v<u2, std::integer_sequence<int, 2, 3, 4>>);

    // swap elements at specific index of a sequence
    using s1 = swap_t<1, 3, std::tuple<int, double, char, float>>;
    using s2 = swap_t<0, 2, std::integer_sequence<int, 1, -2, 7, 4>>;

    static_assert(std::is_same_v<s1, std::tuple<int, float, char, double>>);
    static_assert(std::is_same_v<s2, std::integer_sequence<int, 7, -2, 1, 4>>);

    // sort elements by value in a sequence
    using s3 = quick_sort_t<std::tuple<double, short, double, int, char, char, double>>;
    using s4 = quick_sort_t<std::integer_sequence<int, 2, 1, 0, -3, 4, 1, -7, 5, -2>>;

    static_assert(std::is_same_v<s3, std::tuple<char, char, short, int, double, double, double>>);
    static_assert(std::is_same_v<s4, std::integer_sequence<int, -7, -3, -2, 0, 1, 1, 2, 4, 5>>);

    // sort elements by index in a sequence
    using s5 = sort_index_t<std::tuple<double, short, double, int, char, char, double>>;
    using s6 = sort_index_t<std::integer_sequence<int, 2, 1, 0, -3, 4, 1, -7, 5, -2>>;

    static_assert(std::is_same_v<s5, std::index_sequence<4, 5, 1, 3, 6, 2, 0>>);
    static_assert(std::is_same_v<s6, std::index_sequence<6, 3, 8, 2, 1, 5, 0, 4, 7>>);

    // reverses the order of the elements of a sequence
    using r1 = reverse_t<std::tuple<float, double, int, short>>;
    using r2 = reverse_t<std::integer_sequence<int, 1, 0, 2, -2, 7, 6>>;

    static_assert(std::is_same_v<r1, std::tuple<short, int, double, float>>);
    static_assert(std::is_same_v<r2, std::integer_sequence<int, 6, 7, -2, 2, 0, 1>>);

    // reverses the order of the elements of a sequence recursively
    using r3 = reverse_recursive_t<std::tuple<int, std::tuple<int, std::tuple<char, short>>, char>>;
    using r4 = reverse_recursive_t<std::tuple<char, std::integer_sequence<int, 7, 2, 0, 4, 8>, int>>;

    static_assert(std::is_same_v<r3, std::tuple<char, std::tuple<std::tuple<short, char>, int>, int>>);
    static_assert(std::is_same_v<r4, std::tuple<int, std::integer_sequence<int, 8, 4, 0, 2, 7>, char>>);

    // rotates the elements in the range [begin, middle, end) of a sequence
    using r5 = rotate_t<0, 2, 5, std::tuple<int, char, double, float, int64_t>>;
    using r6 = rotate_t<2, 4, 7, std::integer_sequence<int, 9, 8, 1, 2, 3, 4, 5, 7, 6>>;

    static_assert(std::is_same_v<r5, std::tuple<double, float, int64_t, int, char>>);
    static_assert(std::is_same_v<r6, std::integer_sequence<int, 9, 8, 3, 4, 5, 1, 2, 7, 6>>);

    // returns the elements in the range [begin, end) of a sequence
    using r7 = range_t<1, 5, std::tuple<int, char, float, double, int, short>>;
    using r8 = range_t<2, 6, std::integer_sequence<int, 1, 2, -2, 4, 3, 5, 8, -5>>;

    static_assert(std::is_same_v<r7, std::tuple<char, float, double, int>>);
    static_assert(std::is_same_v<r8, std::integer_sequence<int, -2, 4, 3, 5>>);

    return 0;
}

Introduction

Monster is a metaprogramming library, which is header-only, extensible and modern C++ oriented.
It exhibits a form of pure type programming of compile-time algorithms, sequences and Higher-Order Metafunctions.

Monster provides a conceptual foundation and an extensive set of powerful and coherent tools, that makes doing explict advanced Template MetaProgramming (TMP) in modern C++ easy and enjoyable.

Compiler requirements

The library relies on a C++20 compiler and standard library, but nothing else is required.

More specifically, Monster requires a compiler/standard library supporting the following C++20 features (non-exhaustively):

  • concepts
  • lambda templates
  • All the C++20 type traits from the <type_traits> header

Building

Monster is header-only. To use it just add the necessary #include line to your source files, like this:

#include <monster.hpp>

To build the example with cmake, cd to the root of the project and setup the build directory:

mkdir build
cd build
cmake ..

Make and install the executables:

make -j4
make install

The executables are now located at the bin directory of the root of the project.
The example can also be built with the script build.sh, just run it, the executables will be put at the /tmp directory.

Documentation

You can browse the documentation online at Guidelines.md.
The documentation covers everything you should need including installing the library, a table of contents, and an extensive reference section with examples.

Full example

Please see Tutorial.md.

License

Monster is licensed as Boost Software License 1.0.

monster's People

Contributors

deepgrace 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  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  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  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

monster's Issues

我不能编译通过

在启动生成...
/usr/bin/clang -std=c++2a -fdiagnostics-color=always -g /Users/mashichuan/opt/monster/example/memoized_invoke.cpp -o /Users/mashichuan/opt/monster/example/memoized_invoke -I /Users/mashichuan/opt/monster/include
In file included from /Users/mashichuan/opt/monster/example/memoized_invoke.cpp:12:
In file included from /Users/mashichuan/opt/monster/include/memoized_invoke.hpp:14:

/Users/mashichuan/opt/monster/include/monster.hpp:201:59: error: lambda expression in an unevaluated operand
struct is_base_template_of<B, T, std::void_t<decltype([]<typename... Args>(B<Args...>){}(std::declval<T>()))>> : std::true_type
^

/Users/mashichuan/opt/monster/include/monster.hpp:757:9: error: lambda expression in an unevaluated operand
[]<typename... R, typename... T>(R (*... f)(T...)){}(args...);
^

/Users/mashichuan/opt/monster/include/monster.hpp:809:12: error: redefinition of 'sizeof_t'

struct sizeof_t<T, std::void_t<std::enable_if_t<!is_variadic_type_v<T>>, decltype(T::value + 1)>> : T
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/Users/mashichuan/opt/monster/include/monster.hpp:799:12: note: previous definition is here
struct sizeof_t<T, std::void_t<decltype(T::size())>> : index_t<T::size()>
^

/Users/mashichuan/opt/monster/include/monster.hpp:1019:22: error: expected a type

    using type = typeof_t<next_index_tuple<N - 1>>::template apply<N - 1>;
                 ^

/Users/mashichuan/opt/monster/include/monster.hpp:1019:57: error: expected ';' after alias declaration
using type = typeof_t<next_index_tuple<N - 1>>::template apply<N - 1>;
^
;

/Users/mashichuan/opt/monster/include/monster.hpp:1186:20: error: class template partial specialization contains template parameters that cannot be deduced; this partial specialization will never be used [-Wunusable-partial-specialization]

        struct deduct<T<make_type<m>..., U, args...>> : std::type_identity<U>
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/Users/mashichuan/opt/monster/include/monster.hpp:1185:32: note: non-deducible template parameter 'U'
template <typename U, typename... args>
^
/Users/mashichuan/opt/monster/include/monster.hpp:1185:47: note: non-deducible template parameter 'args'
template <typename U, typename... args>
^

/Users/mashichuan/opt/monster/include/monster.hpp:1421:23: error: missing 'typename' prior to dependent type name 'impl<B1, args...>::apply'
using apply = impl<B1, args...>::apply;
^~~~~~~~~~~~~~~~~~~~~~~~

                  typename 

/Users/mashichuan/opt/monster/include/monster.hpp:3970:52: error: template template argument has different template parameters than its corresponding template template parameter
struct exclude : expand_of<T, set_difference_t<less_t, index_sequence_of_t, indices>>
^
/Users/mashichuan/opt/monster/include/monster.hpp:816:5: note: too many template parameters in template template argument
template <typename T, typename U, auto p = sizeof_t_v, auto q = sizeof_t_v>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/mashichuan/opt/monster/include/monster.hpp:3057:15: note: previous template template parameter is here
template <template <typename, typename> typename F, typename T, typename U,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/Users/mashichuan/opt/monster/include/monster.hpp:5112:88: error: template template argument has different template parameters than its corresponding template template parameter

         using type = type_if<lower == key, std::true_type, impl<l, mid, r, lower, search>>;
                                                                                   ^

/Users/mashichuan/opt/monster/include/monster.hpp:5106:9: note: too many template parameters in template template argument
template <size_t l, size_t r, bool = l <= r>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/mashichuan/opt/monster/include/monster.hpp:5091:61: note: previous template template parameter is here
template <size_t l, size_t m, size_t r, auto lower, template <size_t, size_t> typename search>
^~~~~~~~~~~~~~~~~~~~~~~~~

/Users/mashichuan/opt/monster/include/monster.hpp:7705:46: error: template template argument has different template parameters than its corresponding template template parameter
struct transform_maybe : transform_if<F, P, clear_t, T>
^

/Users/mashichuan/opt/monster/include/monster.hpp:7704:73: note: template type parameter pack does not match template type parameter in template argument
template <template <typename ...> typename F, template <typename ...> typename P, typename T>
^
/Users/mashichuan/opt/monster/include/monster.hpp:7640:69: note: previous template type parameter declared here
template <template <typename ...> typename F, template typename P, typename T, typename U, auto B = 0, auto E = sizeof_t_v>
^

/Users/mashichuan/opt/monster/include/monster.hpp:7900:30: error: template template argument has different template parameters than its corresponding template template parameter
using p = mismatch_t<equal_t, T, U, B1, E1, B2, E2>;
^
/Users/mashichuan/opt/monster/include/monster.hpp:828:5: note: too many template parameters in template template argument

template <typename T, typename U, auto p = sizeof_t_v<T>, auto q = sizeof_t_v<U>>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/Users/mashichuan/opt/monster/include/monster.hpp:3217:15: note: previous template template parameter is here
template <template <typename, typename> typename F, typename T, typename U,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/Users/mashichuan/opt/monster/include/monster.hpp:7901:43: error: use of undeclared identifier 'p'
static constexpr auto q = first_v

;
^

/Users/mashichuan/opt/monster/include/monster.hpp:7903:44: error: use of undeclared identifier 'p'
static constexpr auto l = second_v

;
^

/Users/mashichuan/opt/monster/include/monster.hpp:7927:79: error: value of type 'const auto' is not implicitly convertible to 'size_t' (aka 'unsigned long')
static constexpr auto value = type_if_v<q == E1, std::true_type, impl<q, E1>>;
^

/Users/mashichuan/opt/monster/include/monster.hpp:8248:43: error: template template argument has different template parameters than its corresponding template template parameter
using conc = concat_t<unary_t<store, i, N, T, U>...>;
^
/Users/mashichuan/opt/monster/include/monster.hpp:8107:27: note: template type parameter pack does not match template type parameter in template argument
template <typename... Args>
^
/Users/mashichuan/opt/monster/include/monster.hpp:1665:33: note: previous template type parameter declared here
template <template <typename, typename> typename F, auto M, auto N, typename T, typename U = T>
^

/Users/mashichuan/opt/monster/include/monster.hpp:8249:35: error: use of undeclared identifier 'conc'
using type = concat_t<conc, typeof_t<impl<i + 1, j, std::index_sequence<N...>>>>;

                              ^

/Users/mashichuan/opt/monster/include/monster.hpp:8249:92: error: expected ';' after alias declaration
using type = concat_t<conc, typeof_t<impl<i + 1, j, std::index_sequence<N...>>>>;
^
;

/Users/mashichuan/opt/monster/include/monster.hpp:8361:30: error: template template argument has different template parameters than its corresponding template template parameter
using impl = unary_t<store, i, i, V, U>;
^

/Users/mashichuan/opt/monster/include/monster.hpp:8107:27: note: template type parameter pack does not match template type parameter in template argument
template <typename... Args>
^
/Users/mashichuan/opt/monster/include/monster.hpp:1665:33: note: previous template type parameter declared here
template <template <typename, typename> typename F, auto M, auto N, typename T, typename U = T>
^

/Users/mashichuan/opt/monster/include/monster.hpp:8363:35: error: use of undeclared identifier 'impl'
using type = concat_map_t<impl, T, index_sequence_of_c<min_v<M, N>>>;
^

fatal error: too many errors emitted, stopping now [-ferror-limit=]

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.