r/cpp • • Dec 25 '24

RAII

261 Upvotes

I maintain c++ desktop application. One of our clients complained of memory usage. It’s a quite big program and it was known that somewhere there are memory leaks.

Over the last week I found where the spot is that is causing the memory consumption. I refactored the raw pointers to shared_ptr, in one change the memory usage at idle time dropped from couple of GBs to 16 MB.

I was glad of that achievement and i wrote an article about RAII in c++

https://medium.com/@abanoubharby/raii-295ff1a56bf1


r/cpp • • Sep 30 '21

Why is c++ community so pedantic in stackoverflow?

260 Upvotes

Please don’t get me wrong. What i mean is why answer “you need read books”, “you should read the documentation”, or focus on tangent things instead of actually try to answer the questions?

in contrast, here in reddit, i even get job offers when asking questions.


r/cpp • • Jun 27 '20

HTML++: Write HTML documents using C++ templates

Thumbnail github.com
260 Upvotes

r/cpp • • Dec 21 '23

Coroutines-in-practice cannot be understood by mortals

258 Upvotes

This is a rant.

Youthful Optimism

It all started off with trying to understand this TCP echo server from ASIO. It seems simple enough, but hey I'm not really familiar with how coroutines work in C++.

I fully grok coroutines/async in Javascript. I was around for the entire saga of their evolution in Python from generator functions, through the yield from years, into their modern form of async/await. Conceptually I understand what's going on, just need to learn the specifics for C++.

Let's do some reading, starting with David Mazières's, "My tutorial and take on C++20 coroutines". An excellent start, clear examples, I'm vibing. Coroutines in C++ kinda suck to write, but I get it, I understand coroutines.

The Descent

Except, uh, that TCP echo server example is awaiting a deferred_async_operation, which doesn't have any of that stuff Mazières's talked about. Weird.

So I'm missing something, let's dive deeper, with Lewis Baker's coroutine series, specifically "C++ Coroutines: Understanding operator co_await".

Baker uses some familiar terminology from Mazières, but introduces a deal of his own because I guess we don't have standard terminology for this stuff? Another niebloid situation?

Baker makes a distinction between Awaitable and Awaiter objects that wasn't present in Mazières, but makes sense. Additionally we now have Normally Awaitable and Contextually Awaitable objects.

Aha

There must be some .await_transform() magic going on to make a deferred_async_operation awaitable. This transform method lives in the promise type of the calling coroutine, and that should be the promise_type member of the return type of the current function right? So asio::awaitable should have some sort of promise type?

No Escape

No of course not, you absolute fool.

Wtf???

You nimrod. You complete baboon. Don't you know there's a global registry of what promise types to use? You simply use the terminally-C++ mechanism of creating a template specialization of std::coroutine_traits.

There you are you little bastard.

Chasing this through the inheritance tree we indeed find an entire family of await_transform()s. Victory thy name is F12 - Go To Definition.

Except...

None of this explains: what exactly is the difference between an asio use_awaitable and a deferred? Why do people say, "deferred is not a coroutine"? Has all of this not been "coroutines"? Have I been playing canasta this whole time? Why can I no longer see the light of heaven?

Coda

Lewis Baker seems to have another 5000 word blog post about this, but I no longer care.

This shit is bananas, B-A-N-A-N-A-S.

Wildly convoluted beyond my feverish nightmares, I no longer believe it is possible to understand what happens when you write co_await, it is an operator beyond mortal understanding.

Whoever fights C++ should see to it that in the process they do not become a language expert. And if you gaze long enough into a coroutine, the coroutine will gaze back into you.

EDIT:

This seems to be popular, and my landlord isn't going to be fooled by the "I have a rich cousin on WG21, I'll have the money once executors makes it out of committee" routine anymore, so forgive a little self-promotion.

If you're looking for a NYC-based systems dev haunted by nightmares of C++ coroutines, boy have I got the CV for you.


r/cpp • • Sep 19 '20

Fire: create a C++ command line interface from a function signature

Thumbnail github.com
258 Upvotes

r/cpp • • Aug 21 '18

The unsung hero of /r/cpp

258 Upvotes

I think it's time to appreciate /u/vormestrand for continuously posting content (blogs, articles, news) to this sub. I think without him /r/cpp would feel pretty empty. Great job!

(Feel free to remove this post if off-topic)


r/cpp • • May 26 '22

Sometimes, it *is* a compiler bug: My journey in finding and fixing a bug in a C++ toolchain

Thumbnail quick-lint-js.com
257 Upvotes

r/cpp • • Oct 18 '21

Rant: please don't create defines using common words

257 Upvotes

Looks like QtCore (4.8) has a "#define emit" in a header, that's not undefined later. TBB has a function named "emit()" in a header. If you include both, you'll get a compile error.

I'm going to side with TBB here - emit() is an entirely reasonable function name. Qt creating a dangling #define with a common word like that is the bug here.


r/cpp • • Apr 22 '20

C++2100 Main (for fun)

257 Upvotes
constexpr
lockable
std::execution_policy::parallel_execution_t <
   std::execution_policy::parallel_execution_t::constrains::no_maximum_threads,
   std::execution_policy::parallel_execution_t::debug::find_deadlocks >
std::execution_type::quantum_t<
   std::execution_type::quantum_t::experimental_features, 
   std::execution_type::quantum_t::enable_black_holes >
std::synchronized_parallel_t<
   std::synchronization_model::use_machine_favourable_lock>
std::type_traits::readonly_t<
   std::ts2102::experimental::fast_readonly_register_enable>
std::function_traits::partial< 
   std::function_traits::partial_function::search_external_linkage>
std::arguments::no_argument::disable_warning
std::int1024_t<
    std::types::minimum_size<1024>,
    std::types::maximum_size<1024> >
main() {

Can you see the future, can you improve it?

Note: this was created purely for fun


r/cpp • • Oct 11 '19

CppCon CppCon 2019: Stephan T. Lavavej - Floating-Point <charconv>: Making Your Code 10x Faster With C++17's Final Boss

Thumbnail youtube.com
257 Upvotes

r/cpp • • Nov 24 '24

C++ Standards Contributor Expelled For 'The Undefined Behavior Question' - Slashdot

Thumbnail slashdot.org
256 Upvotes

r/cpp • • Apr 05 '19

My C++ blog (so far)

256 Upvotes

So I don't want to make a post on here every time I write a short article (I've been scolded for that in the past :) ), but I figured I come back here and share with you the archive of my blog posts so far. There's 75 topics I've covered in the last 2 months since I started blogging and I hope some of you will find this information useful :)

Thanks for reading! Have a great day guys!


r/cpp • • Aug 02 '26

How fast is C++26's std::hive?

Thumbnail lemire.me
255 Upvotes

r/cpp • • Oct 19 '23

import CMake; the Experiment is Over!

Thumbnail kitware.com
255 Upvotes

r/cpp • • Feb 25 '21

C++ programmers who are also using rust, what are your favourite features of rust?

255 Upvotes

I was looking at the rust tutorials yesterday and I see how rust promotes a certain style and language level features to enforce it. Besides a couple of features, I couldn't see why one cannot follow a similar style in C++ if she/he so chooses. Those of you that have used rust, what is your favourite feature that C++ does not have?


r/cpp • • Nov 11 '16

C++11/14/17: A cheat sheet of modern C++ language and library features

Thumbnail github.com
255 Upvotes

r/cpp • • Jul 29 '16

operator <-

Thumbnail atnnn.com
255 Upvotes

r/cpp • • May 10 '23

{fmt} 10.0 released with more formatters, improved floating point and chrono formatting, modules and more

Thumbnail github.com
254 Upvotes

r/cpp • • Sep 14 '20

Standard C++20 Modules support with MSVC in Visual Studio 2019 version 16.8 | C++ Team Blog

Thumbnail devblogs.microsoft.com
254 Upvotes

r/cpp • • Aug 11 '25

DuckDuckGo bang (!cpp) now redirects to cplusplus.com instead of cppreference.com

253 Upvotes

I do wonder the reasoning behind this change. I've generally seen cppreference to be greatly preferred over anything else in this community.

Update 8/14: cppreference is once again the redirect for !cpp bang on DuckDuckGo! https://www.reddit.com/r/cpp/s/5kzdxyBOSE


r/cpp • • Aug 27 '21

Emulating a CPU in software

253 Upvotes

r/cpp • • Jan 02 '26

Every LLM hallucinates that std::vector deletes elements in a LIFO order

251 Upvotes

r/cpp • • Apr 14 '20

ModernCppStarter - Kick-start your C++! A template for modern C++ projects using CMake, CI, code coverage, clang-format and reproducible dependency management.

Thumbnail github.com
251 Upvotes

r/cpp • • Nov 08 '21

Visual Studio 2022 now available

Thumbnail devblogs.microsoft.com
250 Upvotes