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
8
votes
3answers
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 ...
1
vote
1answer
45 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. ...
3
votes
1answer
124 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 ...
1
vote
1answer
50 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 ...
5
votes
1answer
186 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 ...
2
votes
1answer
45 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 ...
2
votes
1answer
68 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 ...
1
vote
1answer
47 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 ...
4
votes
3answers
947 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: ...
2
votes
0answers
86 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 ...
4
votes
2answers
221 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 ...
3
votes
1answer
75 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....
1
vote
0answers
86 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 ...
2
votes
1answer
288 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 ...
5
votes
1answer
103 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 ...
1
vote
0answers
76 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 <...
4
votes
3answers
372 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 ...
0
votes
2answers
518 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 ...
0
votes
0answers
91 views

Golang parser for code generation and http request binding

After making some changes to an API realized I had to manually rewrite too many sections of code. That started me looking at different code generation techniques. I did a bit with Swagger/OpenAPI but ...
6
votes
1answer
179 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 ...
2
votes
1answer
35 views

Colour-model classes using CRTP

I want to implement color space logic. So I started with creating some structs: ...
7
votes
1answer
181 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 ...
4
votes
1answer
90 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 ...
5
votes
1answer
90 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 ...
4
votes
1answer
194 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 ...
9
votes
2answers
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, ...
7
votes
2answers
113 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 ...
4
votes
1answer
62 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 ...
5
votes
1answer
207 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 ...
3
votes
1answer
100 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-...
4
votes
1answer
150 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 ...
3
votes
1answer
108 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 ...
2
votes
0answers
38 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 ...
5
votes
1answer
169 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: ...
2
votes
0answers
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, ...
3
votes
0answers
415 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: ...
2
votes
1answer
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 ...
6
votes
2answers
291 views

Run-time and Compile-time Versions of Integer Power

I have implemented exponentiation with integer base and non-negative integer exponent for practicing purposes. Of course there is some upper limit what numbers can be exponentiated within the int ...
4
votes
3answers
379 views

SequencedMap which retains insertion order - Design#2

UPDATE: Current best state of a solution incorporating feedback and further development is in this answer. Review on this Design#2 please: Simple wrapper class template for ...
5
votes
1answer
1k views

Wrapper class template for std::map + std::list to provide a SequencedMap which retains insertion order

Review on this draft please: clang-8 -std=c++17 Simple wrapper class template for std::map & std::list (or alternatively std:unordered_map & std::vector) for the purpose of "retaining ...
2
votes
0answers
76 views

Compile time logical operations for web framework in C++

I'm working on a web framework in C++ and there's this part that I need to do something like this: ...
1
vote
0answers
32 views

Overloading both for (non) constant access [closed]

I'd offer a couple of iterator accesses to the class member, both constant and non-constant. I came to the following solution, but it seems to me far more verbose than needed. I'd like somehow to ...
9
votes
2answers
1k views

c++17 compatible std::bind_front alternative

One of the things that has been really exciting me in c++20 is std::bind_front. Using placeholders with std::bind and ...
4
votes
4answers
409 views

static_vector: a vector with stack storage

In many cases, we want a variable-size array like std::vector, but we know an upper limit on the size. In such cases, the vector can be allocated on the stack. We ...
9
votes
2answers
5k views

`printf` improvement with C++ variadic templates

I am currently working through learning C++'s variadic templates and I wanted to create an example use of them that is less trivial than the one in my book. I decided to create a modification of ...
3
votes
0answers
53 views

Tagged Tuple with "special" copying

This is my first serious post on any stackexchange so any criticism is welcome. Github link to the code in this question: https://github.com/yevsev/tagged_tuple Problem statement For a computation ...
8
votes
1answer
207 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 ...
8
votes
1answer
689 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
256 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 ...
5
votes
1answer
452 views

C++17 allocator_traits implementation

Inspired by my earlier question C++17 pointer_traits implementation, I re-implemented allocator_traits under the name ...

1
2 3 4 5 6