-
Stay up-to-date with everything C++! Content directly fetched from the subreddit just for you. Join our group for discussions : @programminginc Powered by : @r_channels
Top 20 Fastest Cars In The World (SSC, Devel..)
https://7r6.com/sljV
​
https://redd.it/17yhi67
@r_cpp
GUI C++ programming for Windows 3.11 in 2023 (the easy way, with emulation integrated with IDE)
https://www.youtube.com/watch?v=d6L272OApVg
https://redd.it/17yddg8
@r_cpp
Create a website from scratch in C++
https://youtu.be/A9KZ-oXdFA0?si=OTu5RUq0uAyyrtB0
https://redd.it/17y5api
@r_cpp
I Added FP to C++
GitHub
# What Do I Mean by FP?
When I talk about Functional Programming, I'm talking about expression-oriented programming. I'm talking about using various methods, primarily lambda calculus, to minimize imperatives and replace them with expressions.
# Doesn't C++ Already Support FP?
Kind of. Since C++11, most C++ compilers have included the #include <functional> directive, which allows you to declare anonymous procedures with capture statements. However, these are still mere procedures, and not pure functions. They aren't expressions, and they're famously difficult to use. As such, I wanted a purer, cleaner syntax as well as a more direct feature set.
# What Does it Do?
I've added a few features to the language. This is built on the <functional> library, since it helps clean up a lot of stuff with procedures, but it isn't just different syntax for the same thing.
Firstly, I added the most basic thing: function variables, and function literals (lambdas). These are declared with the syntax func fName = lambda(type boundVar).expression;. This syntax is nearly identical to Alonzo Church's original notation, just with a type system added, parenthesis for readability (which he used, if inconsistently), and the keyword "lambda" instead of the greek letter, to make things easier to type. Another difference is that lambdas can have multiple arguments. Automatic currying is a planned feature, but it's not been added yet.
Secondly, I added uninitialised functions, and higher-order functions by creating the lambda<> wrapper. The lambda wrapper allows you declare function types with the syntax lambda<outputType of params>. Of course, since functions can be curried, you can end up with types of lambda<int of lambda<int of int>> or something along those lines.
Thirdly, I added currying. For example, to declare a linear function constructor, you can just write func linear = lambda(float m).lambda(float b).lambda(float x).(m * x + b). To call such a function, you can write func TwiceXPlusSeven = linear(2)(7) to generate a new function.
# How Does it Work?
Simply run your code through the "interpret" procedure (provided in the repo) and it'll generate normal C++ code. Due to this method, it's interoperable with UE5, OpenGL and anything else in the C++ ecosystem.
https://redd.it/17xwp1c
@r_cpp
Beep() stops beeping
I have a little script, that produces 20 beeps. Thats all I want. But no, whoever made CPP had a cock up their ass. It works just fine, but halfway, it will stop beeping. Here my code
#include <iostream>
#include <windows.h>
using namespace std;
int main() {
for (int i = 0; i < 20; ++i) {
Beep(450, 400);
Sleep(10);
}
return 0;
}
https://redd.it/17xqv57
@r_cpp
GWP-ASan: Sampling-Based Detection of Memory-Safety Bugs in Production
https://arxiv.org/abs/2311.09394
https://redd.it/17xnu6z
@r_cpp
C++ IDE for Windows 10 Requiring no External Programs
I have been trying to learn C++ and have not been able to et a compiler to work. VS does not work I have 4 errors in the launch file by default preventing anything from running with little no no information on any of them online and no solutions that worked. Eclipse requires a separate program to work and is covered with settings that I don't know hot to use or what they do or what is needed. I cant install files that require an admin password because I m using a school computer. Is there a compiler that is beginner friendly.
https://redd.it/17xi7oi
@r_cpp
Performance comparison for different ways to copy one std::vector to another
Interesting benchmark for different ways to copy one std::vector to another. Initializing vector with constructor performs 4 times faster than std::copy and memcpy().
https://quick-bench.com/q/KMXsMnOk321ybMDSdoNTpqFW9t0
​
​
https://redd.it/17xgez6
@r_cpp
Build a ufw (firewall) UI in Linux.
I have a project the one mentioned But I'm confused what technology should I use c++ with qt or to pick some random python framework What do you suggest ?
Project description
user interface for a firewall. The user must be able to modify the IP table rules directly from the interface. The interface should have a custom rule button where the user is allowed to insert their own rules, a button to delete/replace/insert rule. and 4 other custom buttons that have preconfigured example rules (stop DDoS attacks, stop all incoming ssh requests, reject http request, etc.)
What is important for me is the gui itself for now even if the ufw doesnt work the right way its ok.
Waiting your recommendations 🫡🫡
https://redd.it/17xb8tm
@r_cpp
Counting
how would I count the number of vowels in an input.
I can't comprehend what my teach is explaining, I've watch the same lesson for a total of about 4hrs.
Here's what I have but it doesn't work. .
int main( )
{
char V(10) = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' };
string Words;
int NumV =0;
cout << "enter sentence here.. " ;
getline (cin, Words) ;
for (index = 0; index < Words.size; index++)
{
if(Wordsindex == NumV)
cout << " There are" << NumV << " in this sentence " << endl ;
}
return 0 ;
}
https://redd.it/17ijn4n
@r_cpp
A Proposal Fix for C/C++ Relaxed Atomics in Practice
https://lukegeeson.com/blog/2023-10-17-A-Proposal-For-Relaxed-Atomics/
https://redd.it/17ht31y
@r_cpp
constexpr std::string | MSVC
Good day!
I'm using the msvc compiler with /std:c++latest.
Judging by the data from the Microsoft website, the ability to create a constexpr std::string was introduced quite a long time ago, but how to create such a string is not obvious.
>P0980R1 constexpr std::string VS 2019 16.10."
There is a similar situation for 'std::vector'.
I tried to create a string inside the 'constexpr' function
1. on site
2. using 'constexpr' functions
Nothing happens unless the string is short. It’s clear that memory allocation on the heap is required.
The compiler throws an error
>error C2131: expression did not evaluate to a constant
message : (sub-)object points to memory which was heap allocated during constant evaluation
How can long strings be formed at compile time, concatenated and converted?
​
https://redd.it/17hqbb6
@r_cpp
Are you a modern or classic C++ programmer?
>What's modern C++? This: https://github.com/AnthonyCalandra/modern-cpp-features
By "modern" I mean: do you tend to use modern C++ features, like things that were only introduced in C++11 and newer versions of the standard? (Examples in the link above)
By "classic" I mean: do you tend to avoid most or all modern features?
View Poll
https://redd.it/17h52w5
@r_cpp
Should we ban polls from r/cpp?
View Poll
https://redd.it/17hcaux
@r_cpp
Working on my own custom functions, this is the result of my sort vs std::sort
I am working on creating my own custom library. So far I just have a few things. But I am pretty impressed with the results of my custom sorting algorithm using my custom vector vs. std::vector and std::sort.
My custom implementation for my sorting algorithm can be found here if you want to look at the code: https://github.com/nathandavis18/Projects/blob/main/Data%20Structures/Custom/sort.hpp
Obviously my custom implementations are far from perfect, and they are still a WIP.
But, the results that I collected after running a test program \~50 times with a vector size of 10 million ints, my custom::sort is on average only 1.2 - 1.3 seconds slower than std::sort, which I think is pretty good.
​
The code that I use for the testing script:
#include <vector>
include <iostream>
include <random>
include <algorithm>
include <chrono>
include "sort.hpp"
include "myVector.hpp"
int main(){
clockt start1, start2, end1, end2; double timetaken1, timetaken2;
static std::defaultrandomengine gen;
gen.seed(std::chrono::systemclock::now().timesinceepoch().count()); //random seed each run
static std::uniformintdistribution rand(1, 10000000);
custom::myVector<int> base(10000000);
std::generate(base.begin(), base.end(), () { return rand(gen); });
custom::myVector<int> customSort(base);
std::vector<int> stdSort(10000000);
for(int i = 0; i < stdSort.size(); ++i){
stdSorti = basei;
}
start1 = clock();
std::sort(stdSort.begin(), stdSort.end());
end1 = clock();
start2 = clock();
custom::sort(customSort.begin(), customSort.end());
end2 = clock();
bool areEqual = (customSort == stdSort);
timetaken1 = staticcast<double>(end1 - start1) / CLOCKSPERSEC;
timetaken2 = staticcast<double>(end2 - start2) / CLOCKSPERSEC;
std::cout << std::boolalpha << "std::sort and custom::sort produce same result? " << areEqual << "\n\n";
std::cout << "Time taken for std::sort to sort 10 million numbers: " << timetaken1 << " seconds\n\n";
std::cout << "Time taken for custom::sort to sort 10 million numbers: " << timetaken2 << " seconds\n\n";
return 0;
}
​
Average runtime (on my pc) for std::sort: 3.2 seconds.
Average runtime (on my pc) for custom::sort: 4.4 seconds.
https://redd.it/17haibp
@r_cpp
New Djinni generator release, adds deprecation annotation to the IDL
Djinni is a tool that takes an IDL and generates C++ code and bindings for Java and Objective-C. This enables to use C++ code on Android and iOS via Java or Kotline and Objective-C and Swift.
Version 1.4.0 adds a deprecation feature to the IDL.
This is great for change management.
Additionally, some other improvements and toolchain maintenance work has been done
The full changelog is here: https://github.com/cross-language-cpp/djinnigenerator/releases/tag/v1.4.0
If you do not know what djinni is, you can find all the information here: https://djinni.xlcpp.dev
https://redd.it/17yeo8l
@r_cpp
Update: C++ DataFrame vs. Polars
You have probably heard of Polars DataFrame, presented as “blazingly fast” DataFrame both in Rust and with zero-overhead in Python. So, I finally found some time to learn a bit about Polars and write a very simple comparison with C++ DataFrame vs. Polars.
I wrote the following identical programs for both Polars and C++ DataFrame. I used Polars version 0.19.12. And I used C++20 clang compiler with -O3 option. I ran both on my, somewhat outdated, MacBook Pro.
In both cases, I created a data-frame with 3 random columns. The C++ DataFrame also required an additional index column of the same size. Polars doesn’t believe in index columns (that has its own pros and cons. I am not going through it here). Each program has three identical parts. First it generates and populates the 3 columns with 300m random numbers each (in case of C++ DataFrame, it must also generate a sequential index column of the same size). This is the part I am not interested in. In the second part, it calculates the mean of the first column, the variance of the second column, and the Pearson correlation of the second and third columns. In the third part, it does a select (or filter as Polars calls it) on one of the columns.
Results:
The maximum dataset I could load into Polars was 300m rows per column. Any bigger dataset blew up the memory and caused OS to kill it. I ran C++ DataFrame with 10b rows per column and I am sure it would have run with bigger datasets too. So, I was forced to run both with 300m rows to compare. I ran each test 4 times and took the best times. Polars numbers varied a lot from one run to another, especially calculation and selection times. C++ DataFrame numbers were significantly more consistent.
Polars:
Data generation/load time: 33.632361 secs
Calculation time: 13.847175 secs
Selection time: 1.847175 secs
Overall time: 48.678397 secs
C++ DataFrame:
Data generation/load time: 28.8234 secs
Calculation time: 2.30939 secs
Selection time: 0.762463 secs
Overall time: 31.8952 secs
For comparison, Pandas numbers running the same test:
Data generation/load time: 36.678976 secs
Calculation time: 40.326350 secs
Selection time: 8.326350 secs
Overall time: 85.845114 secs
Polars source file
C++ DataFrame source file
Pandas source file
^(Disclaimer: I am the author of C++ DataFrame)
https://redd.it/17yah86
@r_cpp
C++20 Concurrency on the Sega Dreamcast
https://twitter.com/falco_girgis/status/1725379035885158457
https://redd.it/17y0v9p
@r_cpp
JLF_UdtRanges - The First Ever UDT Member Algorithm Library for C++
\- Automatically skips members that are unable to be operated on.
\- Works with members of all different types.
\- Matches the STL/Ranges API for consistency and ease-of-use.
\- Extremely versatile.
[https://github.com/jlfdev/UDT-Ranges](https://github.com/jlfdev/UDT-Ranges)
Example:
struct MyUdt {
size_t firstNum = 123;
size_t secondNum = 0;
bool boolean = false;
const char* pChar = "this";
std::string string1 = "test";
std::string string2 = "test";
std::array<char, 5> arr = { 1, 2, 3 };
std::vector<std::string_view> vector = { "this", "is", "a", "test" };
};
MyUdt udt;
size_t position = JLF::UdtRanges::FindFirstOf(udt, "test", [&](auto&& member) { JLF::STD::TryAssign(member, "Test123")});
https://redd.it/17xr2no
@r_cpp
Remedial C++ 17 Library Features - Part 2 - NWCPP 2023
https://www.youtube.com/watch?v=y3uDSkvZRT4
https://redd.it/17xp5hg
@r_cpp
JLFADS - The First Ever Completely Automatic Deep Serialization Library for C++
\- Completely automatic, without any setup or boilerplate required.
\- Easy to use; as simple as a single function call.
\- Reconstructs and reallocates the object and it's members recursively (not just a memory copy).
\- Works with even complex UDT's.
\- Extremely fast and efficient.
[https://github.com/jlfdev/Automatic-Deep-Serialization](https://github.com/jlfdev/Automatic-Deep-Serialization)
Example:
struct MyUdt {
bool boolean = false;
sizet number = 0;
float decimal = 0.f;
std::pair<std::string, std::string> pair;
std::vector<std::string> vecOfStrings;
std::uniqueptr<std::string> uniquePtr;
};
MyUdt udt{ true, 123, 1.23f, {"abc", "def"}, {"test", "this"}, std::makeunique<std::string>("woah") };
JLF::ADS::SerializeObject(udt, "serializationtest.txt");
MyUdt newUdt = JLF::ADS::DeserializeObject<MyUdt>("serializationtest.txt");
​
https://redd.it/17xmk5n
@r_cpp
Mongodb with QT (C++)
I am having a really hard time figuring out how i can like up my project in qt with mongodb cluster online. I tried to follow the mongocxx tutorial but keep getting errors and have had tried a bunch of times. Im on Windows as the system.
Any suggestions? The project has basic CRUD operations.
https://redd.it/17xiec4
@r_cpp
I have question about memory.
Im just learning c++ and i end up in pointers.
I have written something like this...
std::string var2 = "hello world";
std::string * ptr2 = \&var2;
*ptr2 = "WELP";
on breakpoint after WELP it looks more like WELP . WORLD.
in hex ... 57454c500020776f726c64
So question is why part of "HELLO WORLD" - "WORLD" is still there ? Can something done be it or it's just because it's not really allocalized like in C for some number of characters ? I don't understand this.
​
https://redd.it/17xd7iz
@r_cpp
C++ Pitfall : shared_future
https://sheep.horse/2023/11/c++_pitfall_-_shared_future.html
https://redd.it/17xaan9
@r_cpp
Lua Script Manager
The Lua Script Manager is a C++ library that provides a convenient interface for managing Lua scripts and interacting with Lua functions from C++ code. It offers a set of functionalities to compile and execute Lua scripts, register C++ functions as Lua functions, and handle Lua values.
Please let me know if importent Features are missing 🙂
I know the repo still lacks good documentation on how to use it properly. And how to build it properly with cmake in an project.
https://github.com/RealFaceCode/lua.git
https://redd.it/17ihv0a
@r_cpp
What is the meaning of "size" in the code "static_assert(size(int) >= 4)"?
This code is from the book "C++ core guidelines explanation written by Rainer.
his original text is
>P.5: Prefer compile-time checking to run-time checking
What can be checked at compile-time, should be checked at compile-time. This is idiomatic for
C++. Since C++11, the language supports `static_assert`. Thanks to static\_assert, the
compiler evaluates an expression such as `static_assert(size(int) >= 4)` and produces,
eventually, a compiler-error.
My question is :
What is the meaning of “size”
of the code:
`static_assert(size(int) >= 4)`
?
Or it's just a typo, and that "size" should be replaced to "sizeof"?
https://redd.it/17hrw5j
@r_cpp
State of static analysis in the C++ world
After this previous poll https://www.reddit.com/r/cpp/s/ZXk5C9WMeH
many asked for see results option and voted for clang-tidy because they want to see the results or it was the only tool they used.
I know there many other great tools, but I only kept the top 3 you chose in the previous poll.
I appreciate if you can upvote so more people can see and vote. This way the results become more relective and less bias.
Last poll I promise ... Enjoy!
View Poll
https://redd.it/17hkw5y
@r_cpp
[C++20 coroutines] Adventures in Thread-per-Core Async with Redpanda and Seastar by Travis Downs
https://www.youtube.com/watch?v=pOIoeFuIUKQ
https://redd.it/17hdnx2
@r_cpp
Use of c++20 modules for a big c++ solver rewrite
Hello to all,
at work I'll be leading the rewrite from scratch of a big c++ solver (500 000 LOC) over the next 2 years. Environment is linux so gcc/clang.
Of course I'd like to push for the latest standard, however I am not sure about the use of modules, mainly because cmake, clang and especially gcc do not look ready yet.
On the other hand wasting half a second on each include of <algorithm> hurts on a big codebase
What would you do ?
View Poll
https://redd.it/17h3w7r
@r_cpp
Which version of C++ do you consider to be "modern" ?
Not necessarily which version are you using (by choice or necessity), but which version do you consider to be worthy of the name "modern" ?
View Poll
https://redd.it/17h9dsc
@r_cpp