Questions tagged [template-meta-programming]

For code that uses templates to generate compile-time executed code. For ordinary use of templates, use the 'template' tag instead.

Filter by
Sorted by
Tagged with
1 vote
2 answers
43 views

C++ variardic universal template for unknown types, used to handle multiple network protocols

I am creating a template function with variardic arguments, to handle a specific classes that have some interface, method, member or whatever is specialized in a specialization area. However I came to ...
user avatar
3 votes
1 answer
92 views

visit for std::any

While solving an online excersise I have written some basic implementation for a visit function that works with std::any. The ...
user avatar
2 votes
1 answer
38 views

Binding move-only objects to functions (for a thread pool implementation)

I recently implemented a thread pool using std::functions. I only supported functions taking no arguments on the assumption that there would be no need to have them ...
user avatar
  • 21
3 votes
1 answer
72 views

Mapping between Enum <-> Enum <-> String view

I've been working on some kind of mapper that would allow me to map between types. My objectives: it should allow mapping between two enums it should allow mapping enum to string_view it should allow ...
user avatar
  • 241
2 votes
1 answer
69 views

An rough implementation of `std::is_constructible`

As a challenge/fun activity/task, I have implemented my version of std::is_constructible. Here is the source code: ...
user avatar
2 votes
1 answer
65 views

Implementation of versatile IIR digital filter in C++

I have realized that all the digital filters of the IIR type have the same structure. They are described by difference equation in following form: $$ y(k) = b_0\cdot x(k) + b_1\cdot x(k-1) + \ldots +...
user avatar
  • 223
2 votes
0 answers
57 views

invocable_traits v5

Goal: implement traits that for anything callable return its arity, return type and the argument types. Since pointers to data members are also callable, those should be handled (and be considered 0-...
user avatar
1 vote
1 answer
60 views

invocable_traits v4

Update: there is a new version of this code: v5 is posted here Goal: implement traits that for anything callable return its arity, return type and the argument types. Since pointers to data members ...
user avatar
3 votes
0 answers
42 views

invocable_traits v3

Update: there is a new version of this code: v4 is posted here Goal: implement traits that for anything callable return its arity, return type and the argument types. Since pointers to data members ...
user avatar
3 votes
1 answer
46 views

invocable_traits

Update: there are new versions of this code: v3 is posted here and v4 is posted here Goal: implement traits that for anything callable return its arity, return type and the argument types. Since ...
user avatar
2 votes
1 answer
133 views

c++20 compile time string utility v2

This is a follow up on c++20 compile time string utility, as suggested by G. Sliepen posted as a new question, so it can be reviewed on its own. The following code has suggested improvements from the ...
user avatar
  • 65
4 votes
2 answers
308 views

c++20 compile time string utility

While upgrading some meta-program running in production for years to the latest (c++20) standard, I rewrote this particular compile time string utility. Even though this new version produces desired ...
user avatar
  • 65
1 vote
2 answers
101 views

Automatically registering a class using header-only templated classes

I'm trying to reduce the boilerplate of a lot of header-only classes I'm using. Each of these classes must go through a registration step. I want this step to be defined in the same file as the class ...
user avatar
  • 541
1 vote
1 answer
50 views

One-time dynamic, many-time *almost* static type dispatch

Annoyed at the tension between good software design principles that require well-defined delimitations between interface and implementations, and the requirements for critical code to run fast, which ...
user avatar
  • 141
8 votes
3 answers
1k views

C++17 : Typelist manipulation

I'm reading "Modern C++ Design"(A. Alexandrescu, 2002) and practicing basic TMP in chapter 3. Typelists. Since the book is quite dated, I revised example codes in C++17-esque manner. Feel ...
user avatar
  • 1,121
1 vote
1 answer
104 views

template function for conditional dereference

I don't know why there's no such function in std (at least I didn't find one). I need a conditional template function to return a reference to an object of T. ...
user avatar
3 votes
1 answer
204 views

Object to ignore unused function output parameters

In our software we have some functions that return information through lvalue reference parameters, which, in some cases, are irrelevant for the calling code. For that reason, I tried to come up with ...
user avatar
1 vote
1 answer
91 views

Handle C++ templates in Qt5

Searching for a solution to my problem I've read many posts regarding templates in Qt (including those on SE) and didn't find a complete solution (or I should say a complete example of a solution) so ...
user avatar
  • 771
6 votes
1 answer
252 views

Implementing apply_each for tuple c++

I'm implementing apply_each for tuple-like object as a general function which may be a combined version of for_each and ...
user avatar
2 votes
1 answer
66 views

Creating a structural type array wrapper to be qualified as non-type template parameter C++

I'm trying to replicate std::array but with modifications that I should put the private data member to the public so the class template ...
user avatar
2 votes
1 answer
75 views

Computing nth Tribonacci number at compile time using C++ template metaprogramming facilities

I have this small C++ program for computing the \$n\$th Tribonacci number at compile-time. My main concern is: is it possible to get rid of the statickeyword (I had ...
user avatar
  • 25.3k
1 vote
1 answer
54 views

A multi key/value type "folded" map class template

I have recently come across a use case where I need to lump many maps into a single container. These maps can have different combinations of key/value types, but they all differ from one another. For ...
user avatar
4 votes
3 answers
1k views

De-/Serialization of structs representing TCP messages

I wrote two template functions to serialize and deserialize structs representing messages to be sent and received via a TCP socket: ...
user avatar
2 votes
0 answers
92 views

Generic way to access private/protected members

Motivation I and the company is working on building C++ SDK automatically for several platforms, including Windows. In summary, I have to test somehow if the private member of function pointers' type ...
user avatar
  • 121
4 votes
2 answers
411 views

C++ Generic Callback class with removable listeners by unique id

I'm quite new to the STL. Does this make sense? Is there a better way of removing the listeners instead of using shared_ptr while keeping the code short and simple? Is there something in the STL to ...
user avatar
  • 160
3 votes
1 answer
104 views

Implementation of the basic matrix operations for embedded application in C++

I have been developing a control software in C++ and for implementation of the control algorithms I need basic matrix operations like addition, subtraction, multiplication and multiplication by scalar....
user avatar
  • 375
1 vote
0 answers
111 views

"tuple with named fields" implementation

I'd like to show you a type I made while experimenting with literal, non-type template parameters in C++20. The idea is to provide a type that wraps up tuple-like types (std::tuple, std::pair, even ...
user avatar
  • 261
2 votes
1 answer
511 views

C++ generic, multi-dimensional linear interpolation template

Use-case in a nutshell: Interpolate fixed-sized data (a lookup table) that provides output values for combinations of a (fixed) number of inputs. The appropriate scaling is done externally, each axis ...
user avatar
  • 783
5 votes
1 answer
164 views

Binary IO inspired by struct pack

About I'm writing a header-only template library for helping with binary IO. It is inspired by pack-d, which is in turn inspired by Python's struct module. I've only finished the output part and ...
user avatar
1 vote
0 answers
101 views

Making std::optional a monad via SFINAE

To practice with SFINAE, template meta programming, functional programming and monads in C++, I decided to try writing a skinny version of the two functions (monadic binding and return) that make <...
user avatar
  • 497
4 votes
3 answers
379 views

Calculate the centroid of a collection of complex numbers

In working on another problem, one component I needed was to calculate the centroid of a collection of complex objects. The well-known way to calculate this is to simply average the real and ...
user avatar
  • 64.1k
0 votes
2 answers
1k views

struct,template arguments, C++ [closed]

I am writing color model logic in C++. I ask review in defining limits for the color model, by saying Limits I mean this. Consider a struct which represents ...
user avatar
6 votes
1 answer
225 views

Using templates to wrap variardic JNI method calls into type-safe C++ functors

I am working on a project which uses JNI. More than once I fell over the absence of type safety working with the variardic function calls. So I came up with this module. It wraps the variardic JNI ...
user avatar
  • 171
2 votes
1 answer
36 views

Colour-model classes using CRTP

I want to implement color space logic. So I started with creating some structs: ...
user avatar
7 votes
1 answer
215 views

Generic test case templates

I often find that when I am writing, refactoring, or reviewing code that I want to do some simple testing. There are many existing test frameworks such as gtest and cppunit but my desire was to ...
user avatar
  • 64.1k
4 votes
1 answer
121 views

Template Metaprogramming and Modular Arithmetic

Story I'm trying to become more familiar with template metaprogramming and practice more of it. I need to use modular arithmetic to solve certain problems and so I decided to write a general purpose ...
user avatar
5 votes
1 answer
97 views

Compiletime (constexpr) Differential Equation System Description for use in Optimal Control

Background In the field of optimal control, it is part of everyday business to solve ordinary differential equations numerically. A differential equation system or dynamic system describes e.g. the ...
user avatar
  • 306
4 votes
1 answer
252 views

Template Metaprogramming - Multidimensional Vector Declaration

[EDIT] The question has been edited. Please make sure to read the summary at the end of the post. If you'd like me to make a new post with a cleaner explanation and better examples, tell me to do so ...
user avatar
9 votes
2 answers
1k views

How to simplify this C++ CRTP example?

I wanted a simple thing: being able to override a base class and just add one single getter. The bad thing is the base class does override some operators and will return the wrong type. I, therefore, ...
user avatar
  • 933
7 votes
2 answers
139 views

constexpr Precalculate size of serialized data with templates and C++20 concepts

Im writing a serializer that will do serialization/deserialization as fast as possible, and that uses templates so that i dont have to create a ton of functions for every data structure, This is the ...
user avatar
4 votes
1 answer
94 views

Polynomial library using expression templates

In a recent toy project, I made heavy use of calculations involving polynomials in Z[x]. As an experiment, I decided to try out implementing expression templates - and it did seem to provide drastic ...
user avatar
5 votes
1 answer
285 views

Using SFINAE to provide "default" output operators

I am an undergraduate CS student trying to implement simple unit test framework on C++ as a pet project. The framework has an assertion macro like ...
user avatar
3 votes
1 answer
123 views

Const wrapper for iterator

Edit: added clarification for why I want this, and updated the code since I don't have any answers yet I have a C++11 array-like class which (can be) a wrapper around a random-access iterator. Index-...
user avatar
  • 131
4 votes
1 answer
210 views

slab malloc/free implementation

I'm trying to implment memory allocator guided by this tutorial. I used a mix of Next-fit search and Segregated-list search. There are multiple slabs of different sizes (a slab is contagious blocks of ...
user avatar
3 votes
1 answer
121 views

Haskell-like function composition function

Much like this question I'm attempting to write in C++ something which can resemble Haskell's (.) function as much as possible, which is basically Chapter 1's ...
user avatar
  • 497
3 votes
0 answers
45 views

Getting around gsl/span's restriction on temporaries

The goal of this code is to allow temporary RHS variables to be used in a span, specifically as an argument to a function, to be deleted later after the function completes, allowing the passing of ...
user avatar
  • 621
5 votes
1 answer
171 views

Meta functions for sequences of exponents of 2

I have implemented some meta functions to create sequence for exponents of 2. I would love to know if there is any way to make the code more optimized. Note: ...
user avatar
  • 369
2 votes
0 answers
24 views

Using Boost's Runge-Kutta integration capability in a templated mathematical model

This is my first time using Boost and templates in general, so I'm mostly looking for a style critique on my C++. In particular, I'd like feedback on: state_type, ...
user avatar
2 votes
0 answers
576 views

A wrapper for (perfect forwarding + initialization)

Problem Perfect forwarding is known to be imperfect when it comes to list-initialization; I'll use std::construct_at as an example: ...
user avatar
  • 8,937
2 votes
1 answer
31 views

Group built in types to category [closed]

I've created the following program to assist me in some template stuff I'm working on. The function get_type returns 0, 1, 2, 3 or ...
user avatar
  • 490

1
2 3 4 5 6