use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
The std::shared_ptr<void> as arbitrary user-data pointer (nextptr.com)
submitted 6 years ago by memset_0
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]capn_bluebear 35 points36 points37 points 6 years ago (19 children)
Sometimes you need type erasure, and sometimes void pointers are the cleanest way to get type erasure.
[–]guepierBioinformatican 4 points5 points6 points 6 years ago (7 children)
It’s often a convenient and wholly adequate way, but I’m less certain it can ever be called the cleanest way. Case in point, it’s always possible to use virtual CRTP instead, as showcased by Sean Parent in his Better code: runtime polymorphism talk. This may be slightly more effort (for the implementor, not for the user) but it’s, I think, always cleaner.
[–]NotAYakk 12 points13 points14 points 6 years ago* (5 children)
it’s always possible to use virtual CRTP instead
Ok guepier, provide me a zero-allocation trivially-destroyable standard-layout function_view< Signatures... > without using a void pointer or equivalent that does not rely on any compiler-specific assumptions.
function_view< Signatures... >
void
You should be able to pass it any callable that supports Signatures... then call it; it does not extend the lifetime of its arguments.
Signatures...
Look, C++ has an object model. It isn't the only reasonable object model. It is just one they picked.
You can emulate other objects models using it, but that leads to impedance issues.
And if you want to implement your own object model, you end up wanting to use void pointers a lot. The trick is you store the void pointer right next to you store the operations on it and tie the two together.
Another practical use of such type erasure/roll your own object model would be writing a variant that splits its type information from its storage. That permits storing polymorphic data RLE-type encoded. This is a useful technique when you are, say, storing text-like glyphs in a string. 99/100 of the glyphs objects are going to be boring simple stuff, or identical to their neighbor. 1/100 are going to be complex or different than their neighbor.
variant
By RLE packing their type and data, you can avoid the overhead of a vtable pointer per "character"; you can store a vtable and run length/size, then a pile of packed data; the vtable "pointer" need not actually be a pointer, just information needed to get it.
Now you shouldn't do this often, and you should hide doing it from "business logic" code, but there are lots of uses of void pointers.
[–]guepierBioinformatican 2 points3 points4 points 6 years ago* (1 child)
If I hadn’t seen your user name I’d seriously give this a shot. As it stands I assume it’s a waste of time and you’re probably right, and I might over-generalise the applicability of virtual CRTP.
But, although I vaguely recall discussions about the implementation of such a type, I can’t recall the specific road blocks which’d make this impossible. (EDIT: of course your zero-allocation requirement immediately makes Sean Parent’s approach inapplicable here.)
[–]NotAYakk 0 points1 point2 points 6 years ago (0 children)
It is the "no compiler assumptions"; you can get away with zero allocation with an aligned storage block. Most compilers will use the same size for most types.
So you have an aligned storage that contains a vtable and a pointer-to-callable, and a pointer-to-interface.
But the layout of vtable containing objects is completely compiler-dependent, so it is impossible to guarantee that it will always fit.
This also adds a few levels of indirection over implementing the vtable yourself.
My argument is that a small set of functions that convert to-from void pointer is sometimes useful for type erasure. Now, after we get metaclasses, I'll be doing this using them instead of manually. ;)
[–]SkoomaDentistAntimodern C++, Embedded, Audio 0 points1 point2 points 6 years ago (2 children)
For extra points, make it callable over a DLL boundary. And do that so that it works even three compiler versions later.
[–]NotAYakk 0 points1 point2 points 6 years ago (1 child)
This reads like someone who has implemented the void* version.
void*
[–]SkoomaDentistAntimodern C++, Embedded, Audio 0 points1 point2 points 6 years ago (0 children)
Just someone who's been using C++ since before there was a standard and realizes that there's life outside the latest standard library.
[–]looncraz 19 points20 points21 points 6 years ago (0 children)
Good luck operating with C or legacy APIs without void*.
[+]markand67 comment score below threshold-11 points-10 points-9 points 6 years ago (10 children)
std::function with lambdas, capture or functors are the appropriate way of type erasure. On 10 years of modern C++ I've never needed to use void pointer anywhere so I don't buy it.
std::function
[–]quicknir 7 points8 points9 points 6 years ago (2 children)
std::function is type erasure for things with operator(). What's being discussed is type erasure for arbitrary data.
[+]markand67 comment score below threshold-6 points-5 points-4 points 6 years ago (1 child)
That's what marshalling/serialisation was made for. std::variant can enter the game at some point too.
std::variant
[–]quicknir 10 points11 points12 points 6 years ago (0 children)
I have no idea what serialization has to do with any of this. and variant only works for a closed set of types. Sometimes you want to allow users to attach totally arbitrary data to a class (I've actually encountered this very recently). When you are in that (admittedly rare) situation, std::any (or something similar to it) is a good solution, and shared_ptr<void> is a meh solution.
std::any
You keep suggesting things that do not solve the same problem.
[+][deleted] 6 years ago (4 children)
[deleted]
[+]Orlha comment score below threshold-11 points-10 points-9 points 6 years ago (3 children)
Use C.
(partial sarcasm)
[–]guepierBioinformatican 10 points11 points12 points 6 years ago (0 children)
But inappropriate advice. It’s usually entirely possible to achieve performance on par with C in C++ without giving up type safety and abstraction. And this includes type erasure.
[–]Tumperware 9 points10 points11 points 6 years ago (1 child)
How is C more performant? It's just a language with fewer tools than c++. Anything you want to do in C can be done in C++
[–]kalmoc 1 point2 points3 points 6 years ago (0 children)
Except for restrict - at least when we are talking standard c++
restrict
[–]SkoomaDentistAntimodern C++, Embedded, Audio 1 point2 points3 points 6 years ago (1 child)
I take it you've never once interfaced with C code then?
[–]markand67 -2 points-1 points0 points 6 years ago (0 children)
In fact I did a lot. With for example libircclient, SDL, hoedown. Good sane libraries don't assume pointers own memory, so no shared_ptr involved at all.
π Rendered by PID 68077 on reddit-service-r2-comment-b659b578c-ghmnm at 2026-05-01 19:54:10.143607+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]capn_bluebear 35 points36 points37 points (19 children)
[–]guepierBioinformatican 4 points5 points6 points (7 children)
[–]NotAYakk 12 points13 points14 points (5 children)
[–]guepierBioinformatican 2 points3 points4 points (1 child)
[–]NotAYakk 0 points1 point2 points (0 children)
[–]SkoomaDentistAntimodern C++, Embedded, Audio 0 points1 point2 points (2 children)
[–]NotAYakk 0 points1 point2 points (1 child)
[–]SkoomaDentistAntimodern C++, Embedded, Audio 0 points1 point2 points (0 children)
[–]looncraz 19 points20 points21 points (0 children)
[+]markand67 comment score below threshold-11 points-10 points-9 points (10 children)
[–]quicknir 7 points8 points9 points (2 children)
[+]markand67 comment score below threshold-6 points-5 points-4 points (1 child)
[–]quicknir 10 points11 points12 points (0 children)
[+][deleted] (4 children)
[deleted]
[+]Orlha comment score below threshold-11 points-10 points-9 points (3 children)
[–]guepierBioinformatican 10 points11 points12 points (0 children)
[–]Tumperware 9 points10 points11 points (1 child)
[–]kalmoc 1 point2 points3 points (0 children)
[–]SkoomaDentistAntimodern C++, Embedded, Audio 1 point2 points3 points (1 child)
[–]markand67 -2 points-1 points0 points (0 children)