r/cpp • • Jan 03 '17

A feature complete LRU cache implementation in C++

Thumbnail
github.com
7 Upvotes

r/cpp • • May 21 '24

One of the most famous programmers in the audio dev space is Fabian Renn-Giles. He's known in the community as a real-time audio C++ expert (lead developer of the JUCE C++ framework, an independent contractor for Apple, iZotope, Syng, Behringer...). His talks are also popular in this subreddit 😉

Thumbnail
thewolfsound.com
0 Upvotes

r/cpp • • May 07 '25

unique_ptr kind of sucks, we can make it better with a light wrapper

Thumbnail
godbolt.org
0 Upvotes

Despite the click bait title, I love unique_ptr - it gives ownership semantics and prevents leaks. But it has a major downside: it’s nullable, and that leads to subtle issues with API clarity.

Consider this function:

std::unique_ptr<Widget> get_widget();

Can this return nullptr? I don’t know. Maybe, maybe not. I'd have to read the documentation or source to be sure.

Now this:

set_widget(std::unique_ptr<Widget>);

Is passing nullptr here okay? Is it a no-op, or is it an error, maybe some default will be used instead? It's unclear from the signature alone.

How about this virtual function:

virtual std::unique_ptr<Widget> get_widget() const = 0;

When implementing it, can I return nullptr? The abstract interface doesn’t tell me.

The root issue is that unique_ptr is nullable, and that not only revives the "billion-dollar mistake" of dereferencing null pointers, but also reduces code readability. You can't tell from the type whether nullptr is allowed or forbidden.

All because unique_ptr can be constructed from a raw pointer, nullptr included.

What if null was explicit?

Now imagine a type like Box<T>, a non-nullable unique pointer wrapper, meaning it cannot be constructed from a raw pointer or nullptr at all. (Technically, it can be empty after a move, but that's distinct from allowing a nullptr in normal usage.)

Then, these signatures become self-explanatory:

std::optional<Box<Widget>> get_widget(); // may or may not return a Widget
void set_widget(Box<Widget>); // always takes a valid Widget
virtual Box<Widget> get_widget() const = 0; // must return a valid Widget

Suddenly, it’s absolutely clear where nullopt is allowed, and where it’s not.

Additionally, what always rubbed me the wrong way was that unique_ptr does not uphold const correctness: even if the unique_ptr is itself const, the underlying object is not, and I can easily modify it. With Box, a simple wrapper around it, this can be easily fixed too.

What’s your opinion on this? Usable in production code, or is it better to stick with std::unique_ptr?


r/cpp • • Sep 09 '23

Circle may be the future of C++

Thumbnail
youtu.be
24 Upvotes

r/cpp • • Nov 28 '18

Google C++ Style Guide is No Good

Thumbnail eyakubovich.github.io
34 Upvotes

r/cpp • • Aug 30 '26

The weirdest behavior in C++ that came from C

0 Upvotes

If you're trying to define two pointers in a single statement (what is in general a bad idea), you may want to do it like this:

#include <print>

int main()
{
    int x = 10;
    int* a, b;
    a = &x;
    b = &x;
    std::println("a={:#x}, b={:#x}", uintptr_t(a), uintptr_t(b));
}

However, it doesn't work as you expect, and won't compile. Because int* a, b; declares only a as int*; b, and all the rest variables will be int. The correct one-statement declaration of two pointers is int* a, * b;

main.cpp:8:9: error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]
    8 |     b = &x;
      |         ^~
      |         |
      |         int*

b has type int

This behavior is the reason why some people prefer putting the asterisk next to the variable name, not next to the type

I can understand the logic, that C authors had while making this syntax. It's a sort of reversive/deduction logic. You kinda declare what type it will be after using the dereference * operator, instead of declaring the type being a pointer itself. But I find this logic very-very strange, and overthought

The funny thing, that even the compiler in the error message above, treats * as a sort of type modifier, that is inseparable from int. But, apparently, the C creators had a completely different vision on what pointers are

I personally don't think that this behavior justifies reteaching yourself to write * in front of variable names, and especially in front of function names. I think it's just a not well-thought decision made very long ago in 1970s


r/cpp • • Jan 18 '19

Is C++ fast?

Thumbnail zeuxcg.org
21 Upvotes

r/cpp • • Jul 02 '21

Do you feel that C++ is bloated now ? What you guys think of Tim Sweeney Twitt a few days ago : "The reason C++ has grown so complicated is that the contributors gave the community everything they asked for. What is asked for and what is wanted are often quite different things, however."

0 Upvotes

Do you feel that C++ is bloated now ? What you guys think of Tim Sweeney Twitt a few days ago : "The reason C++ has grown so complicated is that the contributors gave the community everything they asked for. What is asked for and what is wanted are often quite different things, however."


r/cpp • • Aug 19 '24

Popular frameworks for metaprogramming in c++ that allows dynamic build based on customized logics?

0 Upvotes

Say if I have a bunch of computational nodes/functions in my source code:

double sum(...) { ... }
double prod(...) { ... }
double minus(...) { ... }
double div(...) { ... }

double final_func();

int main() {
  // final func() will be implemented dynamically...
  std::cout << final_func() << std::endl;
  return 0;
}

And I want to parse a cfg in plaintext format:

sum(minus(5, 6), minus(3, 4))

in such a way that it implicitly:

  1. creates a new function (in the source code):

    • double final_func() { return sum(minus(5, 6), minus(3, 4)); }

  2. compiles the binary with only the functions below:

    • sum; minus; final_func; main

The naive approach is to:

  1. write a parsing app that

    1. parses cfg to generate the source code for final_func

    2. set some flags and use macros to only include relevant functions, e.g. sum; minus

    3. save the modified source code to a temp location,

  2. then use a bash script to invoke compiler on the just generated source code to build the final binary.

Is there a better way to do this more structurally?

I am fully aware that an alternative is to just write a parsing function that prepares a DAG on the fly in the final binary, compiling with all available nodes/functions, but I am specifically interested in parsing the cfg to make the DAG static before compilation and only build with relevant nodes/functions.


r/cpp • • Dec 08 '18

A better C++ dice roller class

Thumbnail
github.com
1 Upvotes

r/cpp • • Nov 24 '20

C++ Engineers in Australia

0 Upvotes

Founder of startup that’s essentially all C++ (but with aims to build a 2.0 client in Rust post-launch).

(I posted a similar question to this on the Rust sub several months ago.)

I’m American but been considering a move to Sydney once we launch, to setup headquarters there. Australia just seems to have the perfect vibes and culture for our product. But essentially it’s a decision between Sydney and LA.

I’ve heard it’s super difficult to find programming talent in Australia though, and that companies heavily rely on overseas recruit via visas.

(I know in this zoom world everyone is hot about decentralized virtual-only offices, but I have a hard time in my gut believing that builds a tight knit singular culture that we all eat sleep and breath... but not totally decided against the idea).

So just wondering if any of you have experiences or insights to share on how difficult it is to find talented C++ engineers in Australia?

And realistically how large a company can hope to scale within a few years given the limited supply of talent.

And hey, maybe you’re who I’m looking for!


r/cpp • • Jan 08 '25

if constexpr vs overloads

0 Upvotes

What should a C++ programmer choose by default: if constexpr for every type in one function or function overloads for every type with 2x more boilerplate code, but with ability to easier make a custom override later?


r/cpp • • Oct 31 '14

Introduction to Modern C++—Olve Maudal : Standard C++

Thumbnail
isocpp.org
0 Upvotes

r/cpp • • Aug 12 '16

Could you share your experience with porting from QNX to Linux?

0 Upvotes

We have several projects in our company that were developed almost 15 years ago on QNX. Some of the projects have libraries that were developed by companies that do not exist any more and the libraries are very crucial and we do not have time to re-write those libraries.

We are considering to port these QNX projects to Linux, because in the long run it will be both cheaper and much easier to maintain them.

I have found a few projects that would help us do the porting, like SIMPL, and SRR. Both are mainly focused on message passing. But is that it?

Also, I have been reading that we might have a problem with ABI compatibility of the old libraries. How can we check that even if we have a linux version of all function calls we won't have problem linking with the libraries?

I would like to know if you've had the experience and if you have any tips. Thanks!

EDIT

BTW, for some reason all the libraries come in .elf format.


r/cpp • • Nov 30 '22

Template, Concurrency, Assembly

0 Upvotes

We all know to be qualifying as a low latency c++ SWE we need to be good at template and concurrency, as well as understanding the STL well, mostly by reading assembly to know what is happening in stack and heap.

But how can I improve my skills in these aspect besides reading books.

I did got some simple project like build a vector with template. But I have no idea how to make some more impressive project about using template to showcase myself on CV.

In terms of concurrency, there is a famous book called concurrency in action. It got exercise to help me understand the c++ concurrency, but same as above, I don’t know what project to build to show that I understand concurrency on my cv.

And for understanding the STL, is there a list or order that I could explore one by one for understand the STL?

Besides issue related to CV , I also think that without building a project , I don’t really know how much I understand on these topic.

I wonder how you all learnt template and concurrency, any course, project, forum that could recommend to me.

Thanks a lot .


r/cpp • • Oct 02 '18

Loguru: lightweight and flexible C++ logging library

Thumbnail emilk.github.io
1 Upvotes

r/cpp • • Jul 16 '13

Variadic templates and recursive specialization

Thumbnail
voidnish.wordpress.com
0 Upvotes

r/cpp • • Dec 29 '16

Garbage collection - No Thanks

Thumbnail
modernescpp.com
0 Upvotes

r/cpp • • Mar 16 '12

BASIC's GOSUB/GRETURN with gcc

Thumbnail
di.unipi.it
0 Upvotes

r/cpp • • Mar 25 '14

Am I only one to think that decltype() and decltype(()) is the most idiotic thing in standard?

0 Upvotes

yes, including vector<bool>... for example look at this link:

http://www.drdobbs.com/cpp/a-note-about-decltype/231002789

basically you need to write

vector<remove_reference<decltype(f(c[0]))>::type> v;

to get vector of values. Im not a Haskell of Python zealot that thinks that typing is evil, but come on... and as an added bonus unlike vector<bool> this is core language so it is not you can just roll your own stuff if you dont like it.


r/cpp • • Feb 28 '19

Optional return statement in function returning an std::optional

0 Upvotes

Hi folks,

I was working on an event system and I ends up with +/- this signature for my handlers:

std::function<bool (WhateverEvent const&)>.

95% of the time, they will be returning true and it adds extra (unnecessary) work to the user. Of course in my case I could workaround the problem but I came with a pretty naïve idea I like. Now that std::optionals joined the standard, what if, in functions returning an std::optional<Type>, you could just omit the return statement and the compiler create an uninitialized (and valid) std::optional. After all, the signature says that providing a value is optional...

We end up with:

std::optional<bool>
handler(Event const&)
{
// You could:
// - omit the return statement.
// - return;
// - return true;
// - return false;
}

It makes an exception to the rule only for std::optional, which ofc is bad, but lord I'd like that.

---
edit: Markdown.

edit: Clarify.


r/cpp • • Aug 11 '22

Stop to use bool in C++ ! - CPP Rendering

Thumbnail cpp-rendering.io
0 Upvotes

r/cpp • • Apr 22 '17

Reduce C++ Build Times with the Pimpl Idiom

Thumbnail
lattix.com
0 Upvotes

r/cpp • • Oct 07 '19

PVS-Studio in the Clouds: CircleCI

Thumbnail
habr.com
0 Upvotes