Questions tagged [template-meta-programming]

Template meta-programming is a meta-programming technique in which templates are used by a compiler to generate temporary source code, which is merged by the compiler with the rest of the source code and then compiled.

Filter by
Sorted by
Tagged with
6
votes
0answers
73 views

Neural Network with Template Metaprogramming

I was implementing a simple neural network, and I noticed that, if I ever wanted to change the layers' activation functions, i would have had to completely rewrite some parts of the code, so I tried ...
7
votes
1answer
85 views

A C++17 std::function implementation

Some time ago I implemented dynamic_array and posted it on Code Review. It used std::function internally for type erasure. This ...
4
votes
0answers
46 views

C++17 implementation of std::invoke

C++17 introduces std::invoke which calls the exposition-only INVOKE. Here is an implementation. This isn't a large amount of ...
4
votes
1answer
61 views

C++17 allocator_traits implementation

Inspired by my earlier question C++17 pointer_traits implementation, I re-implemented allocator_traits under the name ...
7
votes
0answers
47 views

C++17 pointer_traits implementation

pointer_traits is a lightweight trait that provides a uniform interface to builtin pointers and user-defined fancy pointers. That said, things like ...
14
votes
1answer
142 views

Implementation of std::any

Today, I decided to implement std::any using the cppreference page. I've never actually used std::any before and after seeing ...
7
votes
1answer
157 views

C++17 <span> implementation

C++20 added the span library under the header <span>. As voluntary exercise (not homework!), I spent approximately 1.5 ...
3
votes
0answers
80 views

A Node and a Socket, template or inheritance? [closed]

I'm trying to create a node based scripting/computation system. I've come up with 2 solution that seems to work. One involves template, the other one inheritance. Basically what I want to achieve is: ...
18
votes
3answers
2k views

Min function accepting varying number of arguments in C++17

Come across this problem once again in the book The Modern C++ Challenge. 18. Minimum function with any number of arguments Write a function template that can take any number of arguments and ...
14
votes
2answers
2k views

2D counterpart of std::array in C++17

I implemented a 2D counterpart of std::array named array2d in C++17. It is an aggregate like ...
7
votes
0answers
118 views

C++17 is_integral trait implementation

This is a C++17 is_integral trait implementation. Most implementations exhaust all integral types: bool ...
4
votes
1answer
68 views

Builder pattern that forces all members to be initialized at compile-time [closed]

This java answer made me wonder whether it would be possible to implement the builder pattern in C++ in a way that checks at compile-time whether all necessary members of the constructed object are ...
4
votes
1answer
119 views

Symbolic algebra using a generic smart pointer class

I am trying to implement a minimal symbolic algebra library (AKA C.A.S.) in C++ that allows me to utilize delayed evaluation similar to this question from SO. My class hierarchy is getting a little ...
2
votes
1answer
80 views

C++ template-based dimension-tagged type

I'm writing a small C++14 library for myself that allows me to decorate a type with dimensionality information, so that I can leverage the template/type system to avoid mistakes such as adding ...
1
vote
2answers
52 views

Map a set of types to unique IDs and runtime reinterpret back from ID and pointer in C++17

I wanted to create a relatively universal way of serialising an object, by doing a memcpy and generating a unique type ID. Stored together they can be used, for ...
4
votes
1answer
270 views

Defer pattern for constructors in C++

In some specific scenarios, I want to run code after the constructor for a class has run (namely: access std::enable_shared_from_this::shared_from_this()). To solve this without introducing an error ...
4
votes
2answers
109 views

Finding the common iterator category in C++

I created a template trait which finds the common iterator category between a set of given iterator categories. More specifically, it's a class with a member type alias to one of the iterator tags ...
2
votes
1answer
170 views

Boilerplate free registry/factory class for C++

I've made registry/factory class for C++ so I can instantiate different classes at runtime based on some kind of key. My design is partially based on this blog post: http://www.nirfriedman.com/2018/04/...
10
votes
1answer
1k views

C++ math vector template

I wrote a basic template for a vector of arbitrary dimension to be used in a 3D-engine. Most operators make use of C++ template meta programming and instances are implicitly convertible to glm types. ...
1
vote
2answers
58 views

Generic (Multi)SkipList adapter class

Today I have written my first implementation of a SkipList (on top of a std::vector), because I was tired in using ...
2
votes
0answers
50 views

Creating an array of distinct objects with an identical method that can be evaluated (mapped) at compile-time (constexpr)

In a ray tracing project that I'm trying to make compile-time (constexpr) for fun and challenge, I've run into a bit of an issue: I have an object (intersection) that needs to refer to one of a group ...
3
votes
2answers
134 views

Replace a #define/template “Variable Wrapper” system with a pure template/OOP solution

I have an inherited code base that I am looking to improve. Below is a "variable wrapper" template class and #defines that i would like to replace with a template only solution, or redesign ...
3
votes
0answers
45 views

Templatized GLib asynchronous queue

I'm working on a legacy C++ project, which is using GLib for its Main Event Loop. This has caused a lot of ugliness because it mixes C++ and C paradigms (no C++ wrapper for GLib had been used). ...
4
votes
0answers
34 views

Modular graph searcher

some words in forehand: Unfortunately this post is very long, thus I decided to put the whole and compilable code into an online compiler. The link is at the end of this post. In the last few days I ...
6
votes
1answer
459 views

Transaction manager, supporting undo/redo, using C++ templates

I've implemented a basic TransactionManager class that manages undoing/redoing of arbitrary Actions which have access to the <...
7
votes
3answers
485 views

The perfect function alias

For many years, I have waited for function aliases in C++. They still aren't here. I've been using macros to generate wrapper functions. This is my best attempt at the perfect function alias: ...
2
votes
0answers
47 views

SI type safe unit calculations (revised)

This is the continuation of the questions raised in this thread I did include the improvements that were mentioned but still feel like, I do more copies than I need to. Also I am unsure about the ...
4
votes
1answer
70 views

SI type safe unit calculations

I wrote a small type-rich MKS Unit system for the consistent and safe calculation of physical units in everyday use. I realized some operators' implementations via the Barton-Nackman trick while ...
4
votes
2answers
115 views

C++ for_each_adjacent

I was watching a C++ Talk and he showed some code for a basic for_each_adjacent. I took the base concept and improved it a little bit, I am wondering if i could have any feedback. It automatically ...
7
votes
3answers
317 views

Filter template meta function

I wrote a simple C++ template meta function for filtering out types from a list not matching a predicate. This is similar to Filtering variadic template arguments but does pattern matching instead of <...
4
votes
3answers
269 views

Iteration over “zipped” tuples (for_each_in_tuples)

I want to implement for_each_in_tuples that takes a functor and one or more tuples of the same size and applies this functor to <...
3
votes
1answer
250 views

Template Metaprogramming Discriminated Union

std::variant and boost::variant do not require the types involved to be unique, but lose some functionality when types repeat. ...
7
votes
1answer
985 views

Generalized Cartesian Product

Much to my chagrin, neither STL nor Boost has a cartesian product. Namely, given as arguments one or more iterables, create an iterator producing std::tuples of ...
2
votes
1answer
81 views

Extract from typelist by runtime value

I combined the ideas from this answer and a comment to this question to create a new instance of a type extracted from a type list. The index of the desired type within the type list is provided at ...
10
votes
2answers
479 views

FlagSet with C++ templates

I am creating a C++ header-only library for FlagSet. A FlagSet is a container for booleans with meaning. Usually, it is done with constants (macro, enums, whatever), for example : ...
3
votes
1answer
457 views

Simple packet parsing command pattern

I'm currently creating a small server back-end. My goal is to write an (efficient) packet parser in C++17 using the command pattern. The packets are given by: ...
6
votes
1answer
294 views

Compile time positive and negative powers of two

I'm getting started on template meta-programming and I wrote a template to compute compile-time positive and negative float powers of 2. I would like some feedback on readability and overall code ...
3
votes
1answer
152 views

Implementation of has_member_function

From https://stackoverflow.com/a/257382/6949852 I learned how to check whether a member function exists, then I re-write it to standardize the codes: Now the codes become: ...
3
votes
1answer
786 views

C++ Genetic algorithm with templates

This is a genetic algorithm I made. Currently, it seems to be flexible enough for my purposes. Since I am quite new to c++, I'd like to know what you think. I am uncertain about several things and ...
1
vote
0answers
59 views

Type System for Layers and Nodes

Overview The following is a type system of layers (A, B, C, ...
6
votes
2answers
157 views

Compile-time Lagrange polynomials in C++

I have implemented Lagrange polynomials, i.e. \$l_j(x) := \prod_{\begin{smallmatrix}0\le m\le k\\ m\neq j\end{smallmatrix}}\frac{x-x_m}{x_j-x_m} = \frac{(x-x_0)}{(x_j-x_0)} \cdots \frac{(x-x_{j-1})}{(...
5
votes
2answers
272 views

An overloaded random number function template using modern C++

I don't mess around a lot with templates (or with GTK), but today I wrote something and would like to know how it can be improved, and hopefully clarify a few things in the process. So, ...
3
votes
1answer
312 views

Alternative to integer_sequence trick

I hate working with std::integer_sequence. Each time I want to write a function foo I have to write an another function ...
10
votes
1answer
147 views

Automatically implement operator+ using CRTP

I have multiple classes that should support addition. For each of them, the operator += is implemented as T& operator+=(const T& rhs). I also want to add ...
2
votes
1answer
259 views

Asynchronous File System Abstraction

As a side-project, I'm trying to implement a distributed object storage. I've decided to use C++ and libuv, two technologies I had never played with, since it gave me a good reason to learn them. ...
5
votes
0answers
164 views

Custom fixed-space block memory allocator

This is a fixed-size memory allocator. The core memory block is just a unsigned char pointer which gets allocated on initialization. Other than that has pretty basic allocation and block safety ...
4
votes
1answer
43 views

Parsing positive integers from CSV file Part-2

Part-1 here This is a small CSV parser for positive integers mostly intended for personal projects. I (hopefully) improved the code from Part-1 by condensing some parts and improving others while ...
3
votes
1answer
119 views

Parsing positive integers from CSV file

I Needed to parse some positive integers with the following constraints: If possible use C++ only (no C API) Needs to be able to process parsed data after each newline Should be reasonably fast ...
7
votes
0answers
125 views

C++ library to compile-time merge sets to a single array

I built this small library to create a compile-time assembled array from several hard-coded sequences (of unequal length) of a type T: ...
4
votes
1answer
123 views

State logger for optimization problems

I am working on a C++ project for solving a class of optimization problems. The project will be header-only, and for now, I would like to stay with C++11 (so, not fold-expressions). To be able to ...