Questions tagged [c++20]

Code that is written to the 2020 version of the C++ standard. Use in conjunction with the 'c++' tag.

Filter by
Sorted by
Tagged with
23
votes
6answers
4k views

Implementing any_of in C++

I've decided to implement Python's any in C++. I've gone about this using templates to allow multiple types of data to be passed, instead of overloading the ...
15
votes
3answers
3k views

2D Collision Detection in C++

I'm attempting to rewrite the classic snake game in c++. What I am inquiring about is my implementation for a 2D collision detection function. I am utilizing ...
11
votes
1answer
1k views

C++ : B-Tree in C++20 (+Iterator support)

I wrote a B-Tree implementation in C++20, based on my previous Red-Black Tree implementation. Unit Test Demo : https://wandbox.org/permlink/Brw6TgAhdy89OIyj Any feedback will be welcomed! ...
10
votes
3answers
318 views

C++20 standard compatible vector

I decided to write my own vector class template for learning purposes. My main goal was a correct interpretation of C++20 standard (as of n4810). I took some liberties and decided to use concepts ...
10
votes
2answers
799 views

Floyd's cycle-finding algorithm

I am writing a piece of code for some students and I came up with the following implementation of Floyd's algorithm for finding cycles in linked lists. I was wondering if there are ways I can improve ...
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 ...
10
votes
1answer
202 views

Two constexpr ceil functions

ceil_constexpr is is based on: https://stackoverflow.com/questions/8377412/ceil-function-how-can-we-implement-it-ourselves/8378022#8378022 ceil_constexpr2 is a simpler version that takes advantage of ...
8
votes
1answer
323 views

C++20 ScopeGuard

The idea of a defer statement or a ScopeGuard has, for a while, been something I have wanted in C++, but up until recently I had always assumed that C++ did not have it. However, that's when I found <...
7
votes
3answers
611 views

Array based queue implementation with C++20

I implemented a queue that holds elements on an array as underlying data type: ...
7
votes
2answers
456 views

Generic way to remove all duplicates from a not-sorted container

Here's an interesting article called "How to Remove Elements from a Sequence Container in C++". At some point, the author also explains how to remove duplicates from a container, but only with the ...
7
votes
2answers
78 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 ...
7
votes
2answers
262 views

C++2a comparison-based topological sort algorithm

Here's my attempt at a C++2a Standard Library–friendly "topological sort" algorithm. There are several areas of interest here: The algorithm is comparison-based and in-place, just like ...
7
votes
1answer
117 views

Non-nested std::deque and std::list Generator Function for arithmetic_mean Function Testing in C++

This is a follow-up question for A non-nested test_vectors_generator Function for arithmetic_mean Function Testing in C++ and An arithmetic_mean Function For Various Type Arbitrary Nested Iterable ...
6
votes
1answer
103 views

2D Matrix in C++20 and Strassen's algorithm

I wrote C++20 implementation of a simple matrix class and its usage in Strassen's \$O(n^{lg_2{7}})\$ matrix multiplication algorithm. Live demo: https://wandbox.org/permlink/JaSC8fQccFbcl1QY (For n = ...
6
votes
2answers
350 views

Playing with operator<=> (operator spaceship) in c++

I have been trying to get a better handle on the operator<=>, so I wrote two different string wrappers. The Strong_String wrapper uses ...
6
votes
1answer
781 views

Max function with variable number of arguments

I find it irritating that in standard C++ I can't do std::max(a, b) = x when it's possible and that it can't handle more than 2 arguments. For the second concern I ...
6
votes
1answer
425 views

C++20 sort of infinite “Consumer-Producer”

I think I can summarize the idea to the Producer-Consumer problem, with some modifications. And I think I misused the term "Producer" (it depends from which point of view :)) An infinite ...
6
votes
1answer
127 views

A Various Container Type Arbitrary Nested Iterable Generator Function Implementation in C++

This is a follow-up question for std::array and std::vector Type Arbitrary Nested Iterable Generator Functions Implementation in C++ and std::deque and std::list Type Arbitrary Nested Iterable ...
6
votes
1answer
68 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 ...
6
votes
1answer
1k views

Constexpr unit tests and asserts

I'm trying out some different programming styles to experiment with the new C++ concepts. Within the experiment I was looking into how constexpr could be helpful ...
6
votes
1answer
30 views

Filter out comments in Delphi source code

Today I am working on refactoring and documenting some ancient Delphi code. In that language, which is derived from Pascal, there are three forms of comments: ...
5
votes
2answers
122 views

Chunking strings to binary output

A follow-up to this question is Chunking strings to binary block-based output I have code which takes a text file as input and creates a special binary output form of the input. Specifically, the ...
5
votes
2answers
566 views

C++ custom exception class

I am wondering, how can I improve this simple MissingResource exception class? I am using it in my simple game. ...
5
votes
2answers
75 views

sum of multiples by 3 or 5 using ranges

This program calculates the sum of all integers in the range \$[1, 1000)\$ which are multiples of either 3 or 5 or both. Inspired by x86-64 Assembly - Sum of multiples of 3 or 5 the other day, and ...
5
votes
1answer
121 views

A recursive_transform for std::vector with various return type

This is the follow-up question for A recursive_transform Function For Various Type Nested Iterable With std::variant Implementation in C++ and A get_from_variant function in C++. Thanks to G. Sliepen ...
5
votes
2answers
187 views

(de)serializing c++ object almost automagically

Here's one way to solve the serialization problem in c++20, using a json library: ...
5
votes
1answer
140 views

compile-time base64 converter

I have always been amazed at the number of things you can do in a constexpr function, yet I have not thought much about them until recently when I found out that c++...
5
votes
1answer
65 views

Simple pair/vector2 utility class for c++20

I have rewritten the Vector2 class that I usually use in my projects. The code looks as follows: ...
5
votes
1answer
116 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 ...
5
votes
1answer
81 views

Multithreaded console-based monster battle with earliest-deadline-first scheduler

In working on this answer it occurred to me that it might be interesting to further expand the idea. The game This is an extremely simple (and boring!) text-based game that creates a few monsters ...
5
votes
1answer
79 views

Generic Visitor pattern library for polymorphic virtual class hierarchies

Inspired by this cppcon talk from Fedor Pikus about design patterns and some real-world need I implemented a library that allows the user to add a generic visitor pattern capabilities to class ...
4
votes
3answers
355 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 ...
4
votes
2answers
82 views

Nested std::deque and std::vector Type Test Cases for recursive_transform Template Function in C++

This is a follow-up question for A recursive_transform Template Function Implementation with std::invocable concept in C++, A recursive_transform Template Function ...
4
votes
1answer
77 views

A Maximum Function For Various Type Arbitrary Nested Iterable Implementation in C++

This is the follow-up question for A Summation Function For Various Type Arbitrary Nested Iterable Implementation in C++. Besides the summation case, I am trying to implement a ...
4
votes
1answer
73 views

Generically convert bytes to integers

I wanted to know If there was a way to make it more versatile or shorter/simpler. Here's the code: ...
4
votes
1answer
71 views

A recursive_count Function For Various Type Arbitrary Nested Iterable Implementation in C++

This is a follow-up question for A Summation Function For Arbitrary Nested Vector Implementation In C++ and A Summation Function For Various Type Arbitrary Nested Iterable Implementation in C++. ...
4
votes
1answer
288 views

Snowflakes in C++

I've started learning C++ using Microsoft Visual Studio. I'm decided to create a simple GUI program that creates a "snowflake" wherever the user clicks on the screen. Seeing as how this is my first C++...
4
votes
1answer
79 views

Is there a better solution to get RTTI info about a polymorphic type?

I made some code to get RTTI information of a polymorphic object, it works both if you pass it directly or a pointer to it(the pointer will get dereferenced until null or the object is found), it's ...
4
votes
0answers
69 views

An Add/Minus Operator For Boost.MultiArray in C++

This is a follow-up question for An element_wise_add Function For Boost.MultiArray in C++. The following code is the improved version based on G. Sliepen's answer. On the other hand, the built-in ...
3
votes
1answer
142 views

std::array and std::vector Type Arbitrary Nested Iterable Generator Functions Implementation in C++

This is a follow-up question for the previous questions about recursive functions, including A Summation Function For Arbitrary Nested Vector Implementation In C++, A recursive_count_if Function For ...
3
votes
1answer
63 views

C++20 “FixedArray” Container (dynamically allocated fixed-size array)

I've wrote a FixedArray to fit seamlessly (function-wise) with the default C++ containers, including working with algorithms/etc. It is a dynamically allocated, but fixed size array. Code ...
3
votes
1answer
69 views

Linear Interpolation for sorted arrays

I have made a linear interpolation functions as a side project of mine. It assumes everything is sorted before hand - x and f(x) are the same length. I would like to ask for: general recommendations ...
3
votes
1answer
80 views

An arithmetic_mean Function For Various Type Arbitrary Nested Iterable Implementation in C++

This is a follow-up question for A recursive_count Function For Various Type Arbitrary Nested Iterable Implementation in C++ and A Summation Function For Boost.MultiArray in C++. I am trying to ...
3
votes
1answer
78 views

A recursive_transform Template Function with Execution Policy

This is a follow-up question for A recursive_transform for std::vector with various return type, A recursive_transform Function For Various Type Nested Iterable With std::variant Implementation in C++ ...
3
votes
1answer
73 views

An element_wise_add Function For Boost.MultiArray in C++

This is a follow-up question for A Summation Function For Boost.MultiArray in C++. Besides the summation operation of all elements, I am trying to focus on the element-wise operation here. The main ...
3
votes
1answer
83 views

A TransformAll Function For Various Type Arbitrary Nested Iterable Implementation in C++

This is the follow-up question for A Summation Function For Various Type Arbitrary Nested Iterable Implementation in C++ and A Maximum Function For Various Type Arbitrary Nested Iterable ...
3
votes
1answer
79 views

Flatten 2d-array at compiletime

In my code, I need to flatten a two-dimensional std::array into a one-dimensional one at compile time. While writing the code I realized, that I am unsure about ...
3
votes
0answers
43 views

Experimenting with C++20 coroutines to create Python-style generator functions

I've been playing around with the coroutines implementation in gcc 10.2.0 (Debian build) based on the sparse documentation at https://en.cppreference.com/w/cpp/language/coroutines . The target, for ...
3
votes
0answers
149 views

C++20 concepts for container and allocator

I implemented concepts for container and allocator types. I referenced type requirements from here for container and here for allocator. For the sake of simplicity, there are a lot of type aliases ...
2
votes
1answer
108 views

C++ callback multithreaded, can unregister itself

update: a new version of this code is posted here With this post, i would like to 1) ask for feedback on below code as it stands: do i apply all best practices for c++20? is it safe? is my way to ...