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
0
votes
0answers
32 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
37 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
55 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
68 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
78 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 ...
2
votes
0answers
41 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
363 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
50 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
56 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
108 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
34 views

Colour-model classes using CRTP

I want to implement color space logic. So I started with creating some structs: ...
8
votes
1answer
144 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
63 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 ...
6
votes
1answer
76 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 ...
5
votes
1answer
135 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 ...
10
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
88 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
52 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
115 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 ...
4
votes
1answer
81 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
112 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
98 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
111 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
20 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
221 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
30 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
173 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
304 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
670 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
74 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
902 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
331 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 ...
8
votes
2answers
4k 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
0answers
157 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
451 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
208 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
344 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
130 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 ...
18
votes
1answer
617 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 ...
10
votes
1answer
1k 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
86 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 returns ...
15
votes
2answers
5k 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 ...
8
votes
1answer
362 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
276 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
160 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 ...
3
votes
1answer
97 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
2 3 4 5