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.

54 questions with no upvoted or accepted answers
Filter by
Sorted by
Tagged with
9
votes
0answers
385 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: ...
8
votes
0answers
482 views

Simple, intuitive and (hopefully) safe EventDispatcher

For my own game engine I need an event system. I tried to avoid the single-huge-enum-approach for minimizing compile times. The BasicEventListener is the (...
7
votes
0answers
144 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 ...
7
votes
0answers
3k views

constexpr Sin Function C++ 14

I have written a constexpr sin function using c++14 and would like to know what I can do to improve it. I am trying to balance code clarity/maintainability with ...
6
votes
0answers
470 views

Console Command module (in-game console or base for script engine)

I've created a console/terminal command handling module that allows the programmer to bind functions to a command name and later execute them from std::string. What's new is that it handles most stuff ...
5
votes
0answers
86 views

C++ library with tools for (arithmetic) type manipulation and mixing

Broken out from a previous question (C++17 saturating integer (arithmetic) type library) is a small C++17 header-only library to deal with issues that arise working with arithmetic where signed, ...
5
votes
0answers
44 views

ThorsSQL Lib Utility

These are some utility functions that I use for building error messages: SQLUtil.h ...
5
votes
0answers
62 views

Using the llvm::iterator_adaptor_base

I'm currently working with the LLVM JIT framework. There is some graph I had to implement for internal cost bench-marking. It has cyclic node dependencies, so I have to use ...
5
votes
0answers
1k views

C++ class "overload" using variadic templates and wrapped function pointers

I'm using C++11 and I have the following problem (pseudo C++): ...
4
votes
0answers
242 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
0answers
40 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 ...
4
votes
0answers
1k views

Tuple-like container for packed data

I'm writing some code which needs to be compatible accross a Cortex-M based embedded device and x86 / x86_64 based PCs. I needed a solution for sending binary data between the Cortex-M and the ...
4
votes
0answers
130 views

Accept constructor arguments from a given subset of types

I found myself often in the position that I want to have multiple arguments in a constructor which could be in any order. For example: ...
4
votes
0answers
2k views

Generic sliding window

The code implements fully generic sliding window with linear complexity. It should usually be paired with transforming iterator to reach full potential. Sliding window is a grouping of elements by ...
4
votes
0answers
537 views

2D grid container with arbitrary multiple data types

I've been toying with the idea of making a simple game. In game development, it is good to do things in a more struct-of-arrays style rather than arrays-of-structs style, due to cache locality. ...
4
votes
0answers
685 views

Custom basic std::variant

I was amazed by std::variant and got inspired to write my own. For now, it supports constructor+destructor pair, assignment operator and ...
3
votes
0answers
358 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: ...
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 ...
3
votes
0answers
134 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). ...
3
votes
0answers
76 views

Determining if two packs are permutations of each other during compile-time

Often we need to find out if two tuples, or two packs in general, are the same as each other up to permutation. If there are no repeat types, then implementing ...
3
votes
0answers
134 views

Trait class for a serialization framework

In a current project I have implemented a small non-intrusive serialization framework based on type traits. The generic trait template looks like this: ...
3
votes
0answers
419 views

Replacing dynamic polymorhism with static polymorphism in C++

Motivated due to the fact, that the avr-g++ places the vtables in RAM, I wrote a replacement using static polymorphy. Consider the following example: ...
3
votes
0answers
169 views

A way to automatically reorder struct fields by their sizes in descending order

Using constexpr and preprocessor magic it is possible to cook a struct which have a minimal size between all possible sizes, keeping all the data members properly ...
3
votes
0answers
181 views

for_each over multiple containers

I have worked up an implementation of a for_each style function that can iterate over multiple containers at the same time. ...
3
votes
0answers
152 views

Converting a get-by-index function into an iterator for use with STL algorithms

This is a solution to Advent of Code 2016, Day 8. The tricky part of this problem is that you have to be able to rotate both the rows and columns of a two-dimensional array. Rotating a row is easy ...
3
votes
0answers
838 views

zlib wrapper class

I needed to write zlib wrapper class for my application, so I can change it with different algorithm later if I want to do so. Here is the simple version: ...
3
votes
0answers
258 views

Any class implementation

I was inspired from question here, to implement function container that holder any kind of function by using boost::any, I have carried on and extend it further to make the functions holder accept ...
3
votes
0answers
747 views

C++ Event Emitter

I needed an event bus in C++ with a few features: Possibility to add/remove both functions and member methods. Ability to remove automatically those listeners that wrap member methods of expired ...
3
votes
0answers
72 views

Multi-dimensional view of a one-dimensional object

Sometimes, we create a one-dimensional object, but want to access it in a multi-dimensional way. For example, you can access an int[8] array object in a 2x2x2 way. ...
3
votes
0answers
244 views

Invoke callable object passing arguments in "groups of N" using C++17 fold expressions

Interactive Wandbox example Inspired by this Stack Oveflow question, I've implemented a function that, when called with a desired arity, a callable object, and a variadic amount of arguments, calls ...
2
votes
0answers
84 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 ...
2
votes
1answer
225 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 ...
2
votes
0answers
72 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 <...
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 ...
2
votes
0answers
23 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, ...
2
votes
0answers
75 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: ...
2
votes
0answers
54 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 ...
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 ...
2
votes
0answers
480 views

Registering data types with logger at compile time

I am working on a data recorder in my library for pseudo-realtime systems. I have a default templated record() function to cover basic data types, and library users ...
2
votes
0answers
86 views

Bigger coroutine class

This is my attempt to implement the style of coroutines I described in my answer to Small coroutine class. What do you think of it? (My unusual dialect is due to my compiling with ...
2
votes
0answers
218 views

Library for manipulation of binary protocol headers

I'm starting with implementing a TCP/IP stack for embedded systems which will be done in C++ and I've found I need a good way to work with protocol headers involved (e.g. ARP, IP, TCP). Major ...
2
votes
0answers
54 views

Using lists of types to specify types conversion order for operations

I want a mechanism for specifying arbitrary order of type conversion for arbitrary set of types for each operation on that types (separately). For example, let's have set of types ...
2
votes
0answers
39 views

Type traits for ConfigLoaders

I recently added this ConfigLoader class to my project : it loads information (in this example for a window) from an XML config file, using boost. I would like to ask your advice if this is a good ...
2
votes
0answers
91 views

Storage policies

I often come across situations where a library component needs to buffer a large amount of data for later processing. The problem is that at the design stage a choice needs to be made as to whether to ...
2
votes
0answers
113 views

Counting words in files - follow-up 2

Previous question: Counting words in files - follow-up I have fixed punctuation problems in my previous code by using template, because it is easy to understand and my focus on template mechanism. ...
1
vote
0answers
79 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 ...
1
vote
0answers
64 views

Type System for Layers and Nodes

Overview The following is a type system of layers (A, B, C, ...
1
vote
0answers
124 views

Adaptor for a meta-function on sequences to work on packs of types (and vice versa)

Suppose you've written a complex meta-function on sequences of integers and you want the exact same operation to work on packs of types. Instead of writing it all over again, you can use ...
1
vote
0answers
279 views

Breaking recursive cycle in template expression trait evaluation

Context: Within a template expression library (link for reference; warning: work in progress), each expression has a number of traits associated with it. These traits are looked up through simple ...
1
vote
0answers
321 views

Templated string to enum mapping code

I'm trying to create series of template classes that I can use to simplify the process of adding string to enum mapping support. The goal is to minimize the amount of code I have to repeat to define ...