Questions tagged [template]

a design structure for creating several things that are almost identical but need different values. Not template-meta-programming.

Filter by
Sorted by
Tagged with
8
votes
1answer
779 views

Create New C++ Class

I'm currently developing a new version of ExpressGenGen Using TDD. While I'm rewriting the new version of my Code Generator Generator. I'm using its old version to generate code to be used by the new ...
8
votes
3answers
400 views

Generators and Distributions 2.0

In my original question: Generically encapsulating random generators and distributions into a class template I was given some excellent suggestions from two very informative users. I will give a basic ...
8
votes
3answers
996 views

Extended Euclidean Algorithm in modern and readable C++

In Python the Extended Euclidean Algorithm (egcd) could be written as follows: ...
8
votes
2answers
459 views

Template integer range, version 2

This question is a follow-up. The original question was itself a follow-up to an older question by @LokiAstari. The idea is to provide a compile-time integer range. This version incorporates ...
8
votes
2answers
295 views

Python C++ Bindings

I wrote a very simple Python interface for C++ that uses the Python 2.7 C API. At the moment it can: Manage reference counts/deal with "stolen" references (PyObject...
8
votes
2answers
168 views

Type-safe cartesian co-ordinates

I've recently been fiddling around with a type safe implementation of cartesian co-ordinates (and a few operations on those co-ordinates). Often it's easy to get units mixed up: is something in metres,...
8
votes
1answer
1k views

C++ template range

From a previous question I got an answer that included some template magic (that to be blunt was mind-boggling (as I could not understand it)). So I have been trying to achieve the same results (...
8
votes
1answer
264 views

Basic XML template

I am currently exploring the play framework. I'm about to replace the proposed templating system, using the powerful XML processing of the Scala library. Here is what I have come with: ...
8
votes
1answer
994 views

Generic Template implementation of merge sort in C++

A few month ago I wrote several basic sort algorithm for educational purposes as generic as possible in C++. In a few days time I want to present some of that stuff in my class but before that I ...
8
votes
1answer
18k views

Smart enum templates

Here is some code I wrote 10 years ago. I'm now reviewing it and there are a lot of things I don't like. There is possibility for weird behaviour if the template is attempted to be used with a type ...
8
votes
1answer
1k views

C++ template type traits for class members

I have written type traits for checking, if given class T has_member, then if it either ...
8
votes
2answers
188 views

for_each_cons and for_each_slice, variants of std::for_each working on sliding windows

My goal was to replicate Ruby's each_cons and each_slice: ...
8
votes
1answer
606 views

Parsing a CSV file with a very specific format

I'm not very good with templates, so any general tips would be appreciated. Basically this class parses a CSV file with a very specific format. My original idea for this was that I wanted this to ...
7
votes
1answer
580 views

C++ reduce repetitions of code for similar classes

I have the following two classes to make appending strings - and wstrings respectively - easier: ...
7
votes
4answers
7k views

Simple array implementation without bounds-checking

I am preparing myself for some job interviews. This is a simple array (with the error handling left out for brevity). Any input or suggestions on style and the use of templates would be appreciated. <...
7
votes
3answers
5k views

Std lib-like C++ function to find nearest elements in a container

Initial problem For a project I found myself in the need to search for elements in a container that are the closest neighbours to another precise element I have. In my case it was points in any ...
7
votes
2answers
2k views

C++ 2D shape intersections - template reduction

I've created a short piece of code to determine whether or not various 2D shapes (circles/lines/rectangles) intersect. It was a job interview question, but now it's just about self-improvement. I'd ...
7
votes
1answer
265 views

Associative container that produces a unique, instance specific handle for each inserted object

It is not always possible to simplify program design by strictly managing the lifetimes of objects. If two objects have unpredictable lifetimes, but one of them needs to refer to the other, a simple ...
7
votes
2answers
149 views

A C++ template function for doing multi-indexing a sequence

The idea behind the below code is that I can index a sequence with a sequence of indices and get a std::vector of the indexed elements: multi_index.hpp ...
7
votes
2answers
939 views

Generic matrices implementation

I am implementing a litte generic math library. What I have done is to write my generic matrix and vector class. I'm curious if I have it done right so far (implementation wise not totally ...
7
votes
2answers
242 views

Using a CRTP approach for loading OpenGL programs

I wanted to try using CRTP (also new for me) to try making the loading of shared resources implicit. (Specifically I'm using it for loading OpenGl shader programs) This will also help separate some ...
7
votes
2answers
1k views

Sorted vector (aka flat_set) for objects (pointers) with custom embedded key (functor used)

BEFORE YOU READ: this link should be included when you just don't like the format of the question and for those that read this question for the first time, the link may give you the idea what happened ...
7
votes
3answers
1k views

Generic List code with Iterator

I was wondering if this implementation of generic list with iterator is correct. The code compiles and goes fine. What would you think shall I improve? (first time I post here, I'm scared). I'm also ...
7
votes
1answer
156 views

Dynamically call lambda based on stream input: Try 3

Based on Dynamically call lambda based on stream input: Try 2 This fixes most of the points pointed out by @Morwenn. It has been generalized so anybody can call it. The only extra part needed by the ...
7
votes
1answer
163 views

Linear algebra module

I'm working on a linear algebra module to improve my knowledge with mathematics and, because I'll need a lightweight linear algebra module for my future work with Vulkan! I tried to keep a blas-like ...
7
votes
1answer
118 views

A tiny switcher for arbitrary types in C++11

I was about to use the awful enum+map trick to switch on std::strings when I thought to see if I could let some variadic templates do the job for me and I started ...
7
votes
0answers
303 views

Observable Object

I have created an observer and an observable object class. The idea being that you can create an observer that does some action whenever object being observed changes value. Design goals: Be able ...
7
votes
2answers
734 views

SQL code reuse with C#

I have some code that allows for SQL reuse for C#. I'm pretty happy with it, but the fact that I can't find anything similar on the web makes me think I'm going about it the wrong way. I have a GitHub ...
6
votes
4answers
519 views

General advice on a practice linked_list for C++ classes/templates

Introduction I'm learning C++ (Coming from Haskell, C, and Assembly - and other languages sparsely) and this was my practice with classes and templates. It's a linked list that you can call in this ...
6
votes
2answers
1k views

Template for creating 2D games

I am using the following template to program my 2D games in. Is there any way I can improve it? Splash screen: ...
6
votes
4answers
935 views

Generic merge sort in C++ using a user-defined comparison function or the < operator by default

I wanted to implement a generic merge sort algorithm using the same interface as std::sort(). The interface takes first and last iterators to a range of elements in ...
6
votes
5answers
5k views

C++ Stack Implementation Using Templates and Linked List

I have a simple stack class. It uses a linked list for its data structure and it is of type template so any datatype (including pointers) can be passed in. It can be initialized on the stack or on the ...
6
votes
2answers
23k views

Binary Search Tree implementation using templates

I wrote this implementation using templates. It works fine and as expected. Just want to know if this can be improved. I have some specific questions too at the end. Please feel free to critique to ...
6
votes
3answers
1k views

“Set” your expectations low for this ADT

For my third assignment in CS2, I was given the following: A set is a special bag that does not allow duplicates. Specify each operation for a set of objects by stating its purpose, by describing ...
6
votes
2answers
313 views

First template class - Matrix functions

This post has been answered and updated. This was the first time I've ever written a template class. I've spent probably a bunch of times re-writing matrix code for different types, different ...
6
votes
2answers
2k views

Stack implementation using vectors and templates with no overflow version 1.1

I have modified my code as per the suggestion given by some of the developers. Let me know if there are any further concerns or scope for improvements. Original Version ...
6
votes
2answers
392 views

Small Formatting Library

I am learning C++ and currently playing with variadic template functions and formatting string, So here is my small attempt to write a formatting function and provide a simple iostream-like interface ...
6
votes
2answers
924 views

Fixed Point Arithmetics in C++ using templates

I am trying to create a Fixed Point Arithmetics library : I call fixed point a number which has bits reserved for decimal part. Here is the code : ...
6
votes
2answers
460 views

Generic implementation of an array view

I present my implementation of an "ArrayView". I am totally aware the standard nowadays provides such facilities, but for the purpose of learning generic template coding (or is template meta ...
6
votes
2answers
846 views

Template data types implementation

I'm preparing for an exam on C++, as part of my preparation I want to implement a generic map without using anything from the STD for educational purposes. Before jumping into implementation of ...
6
votes
2answers
207 views

Find the most frequent element in a sequence without copying elements

Specification: Given first, last (which are ForwardIterators, and whose ...
6
votes
2answers
134 views

Non-sequential template class container C++

I'm pretty new to templates, and admittedly I'm not particularly good at C++, but I've written a little container that does not move allocated objects once they are created, so you can use weak ...
6
votes
2answers
2k views

Generic pipe and filters

I made a template pipe and filters to replace an old pipe and filters implementation that used inheritance and had a very heavy base class. ...
6
votes
1answer
66 views

Simple bash script creates apache2 virtualhost config for subdomain

Simple bash script to create apache2 virtualhost for localhost. Can be used for public subdomains on developer server with changes - "replace .localhost". Tested on Ubuntu, but should work where ...
6
votes
2answers
2k views

Thread-safe Phoenix Singleton class template with Boost

I've implemented the phoenix-singleton as a class template with boost inspired by Modern C++ Design. The code compiles fine with MSVC 2013 - and it seems to work, but I'm new to multi-threaded ...
6
votes
1answer
434 views

Bitfield class and Register depiction

For a few months now I have been working with an ARM CPU. To be specific an ARM Cortex M3 from STM (STM32107VC). Complete example So far I am using the StdPeriphal library, and will continue to do so ...
6
votes
1answer
1k views

Simple event system

I made this simple event system: ...
6
votes
2answers
643 views

Typesafe scanf-like function with variadic templates

I'm hoping to get some feedback on a function with variadic templates that parses a format string and fills in some parameters whose order and types are based on the characters in the format string. ...
6
votes
2answers
1k views

Type safe program uniform manipulation in OpenGL

I've made an attempt at writing class wrappers around basic OpenGL objects to make managing them easier and more intuitive. Writing a generic one for program uniforms proved to require a little bit ...
6
votes
1answer
8k views

C++ compile-time checked switch 'statement'

In the project I work on there are several places where a switch statement is used on a type enum. (I know, better to use virtual functions or a visitor pattern or something, but sometimes switching ...