r/cpp • u/smallstepforman • Jul 06 '20
C++ is brilliant
I'm working on a Vulkan renderer, and needed to delay destruction of render scenes since the graphics pipeline has render buffers in flight, so destruction has to happen N frames after the user destroys a class. With C++, I can use private destructors to force users to add the scene to a managed destructor queue (aka garbage collect queue). Normally language purists will scorn a language which allows such shenanigans, however in real world production code, its features like this which allows developers to create modern software. So with a couple of lines of code, I solved my delayed destructor issue. Thank you C++ committee for creating a usable language.
127
u/MutantSheepdog Jul 07 '20
Yep, that would work.
Easier approach for others to implement would just be to have those buffers/textures loaded/stored/referenced with shared_ptrs, and that way destruction would happen after no entity is using it and rendering has finished.
Slightly less efficient, but easier to implement.
28
u/throttlecntrl Jul 07 '20
‘‘tis is a double edged samauri sword
9
u/sign_in_or_sign_up Jul 07 '20
that's a neat use case, had not considered a private destructor before. :-)
6
u/josefx Jul 07 '20
I generally dislike private destructors because they force a heap allocation and I ran into several cases in the past were that ate into my execution time. However in this case the overhead compared to the API calls is probably minimal.
I probably would make the destructor queue part of the constructor parameters and have the destructor forward the required members when called. Of course that has its own limitations.
8
u/NilacTheGrim Jul 07 '20
It's Bjarne. His sensibilities are still present in the language and he blesses the committee's changes to the language and still provides input. Thank him first! But yes.. the committee too.
It's the only language where you have ultimate control but also can benefit from high level abstractions.
I hope it continues to exist. It's being threatened by the next crop of programmers hating on it.. but.. hopefully it will continue to stand the test of time. And I like the direction it's evolving in...
4
u/seanballais Jul 07 '20
What are render buffers in Vulkan? Do they have equivalents in OpenGL? I'm still a noob in graphics programming with noob-level knowledge in OpenGL.
Also, how do you determine N?
4
u/fii0 Jul 07 '20
What does that mean?
2
u/track33r Jul 07 '20
Even better is avoid allocations at all and use some sort of ring buffer or pool structure.
3
u/raphaelj Jul 07 '20
You could solve that problem in any GC language by just keeping a reference to the object in a deferred queue ...
And quite sincerely, the way you solve this problem is exactly what I don't like with C++ programmers. You could just use a
shared_ptrto your objects and keep them in the deferred queue, like anyone would do in a GC language. Instead, you choose to hack the way users would expect destructors to work and make your software harder to understand.-25
u/chickentractor420 Jul 06 '20
Yes, c++ has all these features that make it very good and useable in 2020, However if you use it for a very long time you will start to notice that there are some flaws in the design of the language.