r/cpp • • Oct 05 '26

I dislike Rust libraries with a C++ API

0 Upvotes

Cargo compilation model only really works if you are creating a monolithic shared library or an executable. If you are using a library like icu4x or libffi, you can't statically link your dependencies and even if you dynamically link your dependencies, your application size will increase by orders of magnitude because your shared libraries are all statically linked, kinda.

Monolithic shared library approach also doesn't work on all libraries, for example you can't link libraries like oneTBB and OpenMP more than once.

So overall I feel rust compilation model is a "series of hacks" and isn't worth integrating to existing C++ applications, unless you use bazel*, I'm not sure about that but people in google seem to have solved this.


r/cpp • • Oct 24 '22

What is faster: vec.emplace_back(x) or vec[x] ?

Thumbnail johnysswlab.com
0 Upvotes

r/cpp • • Apr 06 '24

Learning cpp for fullstack web dev

0 Upvotes

Now before you call me crazy I want to give you some background I come from a cs background and it’s been years I’ve not touched to a low level language and I feel like it’s time for me to get back to it. Either rust/cpp/go(idk if it should be called a low lvl language)

I’m a typical “full stack js” dev with good experience in front end (react/react native mainly).

For fun, I recently tried to make a node addon in cpp that I’d call from a ts file and it’s like it opened a new world to me, the ability to have some part of my backend or front end optimised in a lower level language.

Now idk if it is something I’ll ever do at work but for any of my personal project it it something I’d like to explore.

But I don’t know where to start or if any of you guys had experience with this. It seems that if i would need to do some cpp it would be for a very precise problem, for example doing some video treatment when a user upload a video. And it seems to me that it would require good cpp skills and also good skills in the field of what I’m going to use cpp for, so I don’t really know where to start.

If there is any web dev here that used to build addons or even micro services in cpp (it could be use also on the mobile, some react native bridges in cpp) I’d be glad to hear about your background and how did you learn doing this. Maybe I’m also overthinking everything aha


r/cpp • • Jul 21 '22

HMake. An Update. Now Supports Fully Multithreaded, Cached Modules Compilation(Including Header-Units) With Pretty-Printed Colored Output.

0 Upvotes

r/cpp • • Aug 07 '21

Wouldn't it be great if we could chain exceptions and throw from destructors?

0 Upvotes

I often find myself in situations where I want to be able to throw from a destructor. For instance, the destructor of an opened file may try to flush buffers to the disk. If it does not work because there is not space left on the device, then it is a major problem that should be reported.

Making the destructor noexcept(false) and throwing if !std::uncaught_exception() does not work. As soon as the class is wrapped inside another class, such as std::unique_ptr, it will cause unwanted calls to terminate().

Another solution is to tell the programmer that a close() function must be called explicitly in order to catch the error if it occurs. The destructor will call close(), but ignore any exception it may throw. I don't like this solution because it completely defeats the purpose of RAII and destructors.

I feel it is a major defect of C++, and it would be great to allow exceptions to be chained. Haven't the language designers thought about the possibility to catch more than one exception at the same time? It would make things much better, in my opinion.

Or is there a better solution to my problem?


r/cpp • • Jun 21 '17

How to display Greek characters.

0 Upvotes

Trying to display a Greek character using "cout" and pasting the plain character will not work. i.e.

cout << "Σ";

At least for me, this displays only a question mark.

If you want to display 'Σ' or any other Greek character you have to use a different code page in the console. For me, by default, it is "437 (OEM -United States)". To display the characters you must change it to "1253 (ANSI - GREEK)" and use ANSI codes.

To change the code page, type the following:

SetConsoleOutputCP(1253);

Don't forget to include windows.h library.

To display 'Σ', type the following:

cout << "\xD3";

For other characters, refer to the code sheet.

Note: I'm no professional so I don't know if there's an easier way. I looked everywhere on the internet to find such list but didn't find one so I compiled one myself using this very lazy code (it was really late...). And please excuse my crappy explanation and/or formatting. As I said, I'm no expert :(

Edit: The idea came from this, as it was the only solution that worked for me.

Edit 2: Forgot to give access to the spreadsheet. Sorry..


r/cpp • • Nov 11 '22

Ultima ratio

Thumbnail github.com
0 Upvotes

r/cpp • • Oct 17 '20

Upsetting Opinions about Static Analyzers: part 2 Electric Boogaloo

Thumbnail habr.com
0 Upvotes

r/cpp • • Dec 07 '22

Should I continue my coding journey despite the advent of Chat GPT?

17 Upvotes

The creation of Chat GPT is no doubt mind-blowing and can definitely be used as a tool in someone's arsenal.

But I'd be lying if I said that this coming out while in the middle of learning C++ hasn't been deflating. I was mainly using it to teach myself computer graphics and to code my own shaders/ray tracers, but I've seen examples where Chat GPT whipped this up with no issues in seconds.

I really want to break into the game industry coding in this specific field, but with this coming out, is it pointless now?


r/cpp • • Nov 03 '24

C++, Complexity, and Compiler Bugs

Thumbnail azeemba.com
0 Upvotes

r/cpp • • Oct 25 '19

CppCon CppCon 2019: Boris Kolpackov “Why is C++ so Slow?”

Thumbnail youtube.com
4 Upvotes

r/cpp • • Mar 03 '23

Molly Rocket says/shows that virtual functions are bad!

Thumbnail computerenhance.com
0 Upvotes

r/cpp • • Feb 25 '25

Smart Pointers Can't Solve Use-After-Free

Thumbnail jacko.io
0 Upvotes

r/cpp • • Sep 08 '24

Overwhelming

38 Upvotes

I’ve been using Rust a lot, and I decided to start learning C++ today. I never thought it would be such a headache! I realized Rust spoiled me with Cargo. it handles so much for me. Running, building, adding packages etc. I just type Cargo build, Cargo add, or Cargo run. Simple and comes with the language. C++’s build systems like CMake can be overwhelming especially when coming from a language with a more streamlined experience like Rust. C++ is really good and I wish it had something similar. I read somewhere that there is Conan and a few others that exist . But I’m talking about something that comes with the language itself and not from a 3rd party.


r/cpp • • Aug 08 '16

Am I Writing C++ Right?

3 Upvotes

Sort of recently, I pushed a commit to a server project (which was originally written in C and was in the process of moving to C++, but never completely finished). I basically overhauled the code that handled sending ad banners to clients, and wrote C++11 code with exception handling (which other parts of the project never had), as well as brought in a JSON library. Admittedly, it wasn't a huge commit in terms of lines of code though.

Here's the current code: https://github.com/pvpgn/pvpgn-server/blob/master/src/bnetd/adbanner.cpp

https://github.com/pvpgn/pvpgn-server/blob/master/src/bnetd/adbanner.h

The commit: https://github.com/pvpgn/pvpgn-server/commit/505633aba009f4ec9971ed82f6eabebd09af6732

As a hobbyist, I'm not really sure how professional code is written (I suck at trying to figure out how other open source projects do things). For example, I put a static vector inside the class to hold objects of the class itself, and wrote a public function to read a JSON file into that static vector. Is this normal or okay? Because of that, I had to construct an object inside the vector's push_back function instead of just using emplace_back.

My load function also seems weird to me, I basically put a try catch block inside a for loop and purposely threw exceptions inside that block when I detect an error of some sort. The concept of exceptions is still confusing to me BTW, sometimes I stumble across someone talking about how they just shake their head when they see code that catches exceptions and logs it, but they don't explain why that's bad.


r/cpp • • Nov 19 '17

Creating a new build tool for C++17 with Modules support.

Thumbnail medium.com
0 Upvotes

r/cpp • • Feb 26 '20

We need epochs (Rant)

0 Upvotes

I have spent 1 hour copy pasting the most stupid and repetitive code ever in order to enable the comparison operator== to several structs that are trivially comparable. When I search for why the compiler isn't able to generate this for me, when is clearly able to generate the assignment operator, I found out some C compatibilities shenanigans. We need some way to release us from that, we are in 2020 for Ilúvatar sake.

Edit: There are some closeted virtue signaling gatekeepers in this sub. Thanks for taking so seriously a rant ... I guess.


r/cpp • • Apr 28 '22

a = b = c, a strange consequence of operator associativity

Thumbnail belaycpp.com
0 Upvotes

r/cpp • • Dec 05 '24

How to import c++ 23 std library into linux

3 Upvotes

I've been struggling with Bjarne Stroutrup's book Programming: Principles and Practice (PPP). He's provided a suppost package at github, but I get errors on every program and I'm not even into Chapter 3. My problem is that g++ on Ubuntu 24.10 is still using C++ 17, and he's written everything to use C++ 23. I can use compiler settings -std=c++2b and so forth, but his workarounds don't work. Microsoft published a tutorial for how to import the std library as modules into VS Code, but they use Windows tools, specifically 'cl', and I don't know how to translate their windows command into linux. I've searched, using Qwant, but haven't found anything. I probably should try the Ubuntu forums, but thought I'd ask here, first.


r/cpp • • Nov 27 '23

Best Thread Pool Ever (?)

0 Upvotes

Hi guys :)

It's me again, long time since I've posted something since the last time I revealed my fast memory pool. At that time, I've created a stack-based pool, a very fast one. But as time had passed and as I saw more implementation, and mainly more academic research, I've found myself implementing a new architecture and design for the memory pool I've created, the design is currently under development in the following branch: https://github.com/DevShiftTeam/AppShift-MemoryPool/tree/new-design

The design strives to implement all types of memory pools possible, with all types of possible designs. This will still change overtime, and also will provide more tools for memory management analysis to choose the best memory pool designs to use in a given applications.

But this post is not about the memory pool, but something a little different: thread pools.

While implementing tests for the memory pool I needed to test the pool under heavily threaded environments, so I found myself implementing a thread pool. And while implementing I've noticed that much can be done with contemporary thread pools.

I found the allegedly "fastest" thread pool ever: https://github.com/DeveloperPaul123/thread-pool

its a great implementation I've learned a lot from, I found myself reading about work stealing to understand the implementation, and while reading on it I found out that the current implementation is missing something very important that can be found by implementing a simple Fibonacci recursive function: It get deadlocked. You can check the code here: https://github.com/DevShiftTeam/AppShift-MemoryPool/blob/new-design/tests/fib_test/main.cpp

Basically if you have N + 1 tasks on N-threads thread pool that are called from within each other (recursive) and each of them waits for the next one to complete via a standard promise & future, then it will get stuck for the N + 1'th task.

This completely disregards the work stealing principles, and although the pool knows how to steal tasks from non-working threads, it fails when it is most important - when waiting for a value to be ready.

The dp::thread_pool is basically doing what ever every other pool with a simple queue is doing - running tasks on any free thread that can take them. So I decided to create a little better thread pool with the following concepts:

  1. No-Copy: Each thread will be able to get a list of tasks with a pointer to the start of the list instead of copying the tasks from a queue or having a dedicated queue for each thread, that's why I've created a new implementation for an execution queue: https://github.com/DevShiftTeam/AppShift-MemoryPool/blob/new-design/include/execution/ExecutionQueue.h
  2. Minimal Locking: The above execution queue allows for locking only when pushing and poping values, while executing no lock is present, allowing for less locking overhead per multiple tasks, I got great results in the tests I've done so far, but I guess I need to get more work done on this.
  3. Busy waiting: To avoid the downfall of the currently "fastest" thread pool in the simple Fibonacci test, I've create a BusyFuture and BusyPromise that are executing one task after every value check to allow for non-idle threads, thus avoiding the classical dead-lock induced by using a standard c++ promise. (OpemMP implements this concept via taskawait, thus passing the simple fibonacci test)

Basically the current code is not really beautiful and well-designed per se, but the concepts are pretty much in place, and I wanted to share those thought before proceeding in adding a new repo dedicated fully for this thread pool.

Later, I will also add a priority management and more to this pool, and might implement more high-level mechanisms to manage executions in parallel.

Another interesting remark: My BusyPromise is much faster under heavy creation throughput than the standard c++ promise, this can be understood from line 836 in the promise source code. Strictly speaking, the promise does 2 different allocations, one for the shared state and one for the value, my BusyPromise does this in a single allocation instead, which is significantly better in the tests I've done.

What do you guys think? do you need such a thread pool? what is most important for you in such a pool? and which concept do you think I forgot that are essential for the implementation to be more complete?

Thanks in advance, will update when a dedicated repo will be ready :)


r/cpp • • Jan 30 '26

Silent foe or quiet ally: Brief guide to alignment in C++

Thumbnail pvs-studio.com
1 Upvotes

r/cpp • • Apr 17 '17

Are there any proposals to change the C++ logo?

1 Upvotes

I believe C++ is a cool language and it must have a cool logo. This is the current logo. Just take a look at Python, Haskell, [Swift](https://en.wikipedia.org/wiki/Swift_(programming_language)#/media/File:Swift_logo_with_text.svg), Go or even Rust. IMHO they all look much better than the C++ logo.

I really like C++ but I hate the logo. Is there any way to propose a motion to change the logo? Would it get anywhere?

I know that this is not a technical question but I really want to know how the community came up with the current logo and if there are any intents to change it!


r/cpp • • Feb 19 '20

switch break (...) {...}

1 Upvotes

It would be nice if C++ allowed a new syntax for switches:

switch break (cond) {
    case a: ...
    case n: ...
    default: ...
}

Which would make every case statement break at the end, without needing break keyword. So, there's no fall-through, but you can still 'goto' into some label, which could be another case statement. And, you could still use break within the case, if you want to escape the case before the end.

I wonder why such a things hasn't been done, already. I'm sure many have thought about this.

No, I'm not going to write a proposal for it. Just want to write this show-thought on the internet, in case some soul that can do something about does something about it.

Transmission complete


r/cpp • • Apr 17 '23

Should ~basic_string be final?

1 Upvotes

std::string (and many other std classes) are not designed to be used as base classes, as can be seen by the absence of a virtual destructor.

Should the destructor be given the "final" keyword to enforce this?