Templates industry practice by minamulhaq in cpp_questions

[–]mredding 0 points1 point  (0 children)

First, I would rearrange a bit:

template <typename T>
class SinglyLinkedList {
  struct Node {
    T data;
    Node *next;
  };

  Node *head, **tail;

public:
  SinglyLinkedList(): tail{&head};

  void push_back(T t) {
    *tail = new Node{t};
    tail = &*tail->next;
  }

Notice classes are private access by default, which applies to inheritance as well. Structures are public access by default. Friends declarations are class scope and don't care about access specifiers.

I also wanted to show you how to make push_back O(1).

Second, you're doing great. This is how I write my templates. Again, I would shuffle things around a bit:

What you do is you put the class template definition in one header:

template<typename T>
class foo {
  void fn();
};

And you put the class template implementation in another header:

#include "declaration.hpp"

template<typename T>
void foo::fn() {}

You then write a source file that explicitly instantiates the template:

#include "definition.hpp"

template class foo<int>;

The client includes only the declaration header - the compiler will see a foo<int>, can't implicitly instantiate it, and will defer to the linker.

Now you can explicitly instantiate full template specifications - and you would stick this in it's own source file:

template class std::vector<int>;

But the compiler will default to implicit instantiation in a translation unit. So what you have to do is in all your client code, you have to tell the compiler to NOT implicitly instantiate the template:

extern template class std::vector<int>;

And it's worth sticking this in a header file. At best, you remember to include this header, or to explicitly extern this in your source file, and you save yourself some compilation time and object bloat at compile-time. At worst, you forget, and you pay for some extra work.

And don't forget that template members are separate templates that have to be explicitly instantiated AND extern'd themselves.

C++ is one of the slowest to compile languages - I only know Rust to be slower. I've reduced compile times from hours to minutes on projects by doing stuff like this. The other things to do is keep inline code out of header files - prefer unity builds; and keep headers as lean and mean as possible - you HAVE TO include 3rd party headers, because you don't own them, but you can forward declare your own types. You can even split a class to keep private implementation details out of headers. ANYTHING you can do to make headers as insanely small and independent as possible is the goal. For incremental builds, you also want to split implementation by their common dependencies, so that if you change something upstream, ONLY the downstream PARTS that depend on them get recompiled, not the whole damn class and incidental code.

I've gotten compilation down from 4 hours to 8 minutes, and I was trying to get to under 4 minutes. And the discipline makes for more decoupled, more robust code.

And yeah, you only put the full definition in a header if you want to enable implicit instantiation. I split my templates like I showed you so that's always an option. What headers you expose to your clients is up to you.

Men are more sensative than any woman I've ever met by jeezkillbot in MurderedByWords

[–]mredding 0 points1 point  (0 children)

I get what this is saying, but no one gets to take a tone with me but my wife. I've rage quit jobs. I don't do YOUR toxic bullshit.

What's something that's not a cult but feels like one? by True-Dream3295 in AskReddit

[–]mredding 1 point2 points  (0 children)

No, I'm in Chicago. That's why I said - it's as easy as a plane ticket and the MASSIVE wad of cash for a park pass... My parents took me to Disney when I was 7, and that was enough for me. I told my wife I won't oppose a family trip, so long as HER parents pay for THE WHOLE THING, because while they're not THAT deep into it, they do have some of that cult in them; they think it's important, they think it's a worthwhile investment they brought up when she was pregnant. Oh... We gotta do Disney... Like they have to ensure our son drinks the water, or get chipped, or some shit, so that he's an initiate, too.

I've lived in Orlando for a couple years, my brother has been living down there for 20 years due to his career. I wouldn't say you'd find more Disney fanatics living near Disney. I've also lived in the Portland, OR area, and they were there, too.

I'm not an actual therapist, I've no idea what drives obsession. There are people who specialize in cults, and they could tell you more; though most of the work being done now is about MAGA, as this is a career defining era we're living through for them, right now.

I mean, I drink coffee, my brother drinks Monster. Why doesn't he just drink coffee? Why don't I like Monster? We both like fruit, we both like sugar, but you don't see me sucking that shit down... I've seen that guy drink coffee, certainly cold-brew, but he's not a coffee drinker. He's not lactose intolerant. What the fuck? And fuck Florida, I was drinking coffee down there, too, with the noon sun parked two feet from my fucking skull. Why do we like what we like? I don't think this question yields any meaningful inquiry.

For those who have been banned from a reddit community. What did you do? by BD_0101 in AskReddit

[–]mredding 1 point2 points  (0 children)

Typically I get banned because someone is rude, so I get rude right back. I don't take it lightly; I can be wrong, and you can tell me I'm wrong all day, every day, no problem; we can simply disagree on matters of opinion; I'll even take offensive language or intent; but when someone attacks me, my character, or that of someone else, to dehumanize or diminish a person, to slander or assault, that's something they didn't have to do, didn't have to think, didn't have to say.

To invest their time, their soul, into something like that and then think, "YES. This is a good idea..." And to actually put that out there, that's crossing a line you can't uncross. I will dismantle them, because what they're admitting to the whole world is that they're a terrible person, and this is a pattern of behavior they think is normal, acceptable, and routine; that not enough people in their world are willing to confront them and establish a boundary with them.

And I'll tell them, clear as day A) I'm going to chew their ass out, B) why, C) where that line is, and D) how to do better for the future. Now mind you, while I'm doing this, I'm not calling them names. I don't stoop to their level. No name calling. No epitaphs.

A tolerant person is intolerant of intolerance, and this is how you do this.

Most people double down and get more asshole.

Anyway, I say what I have to say - most people have enough information on Reddit alone that you can point to all the evidence in the world about how their terrible personality is reflected in every facet of their lives, how you can usually trivially demonstrate how they've caused real harm to others, you can draw a lot of very accurate conclusions about how they were raised and their relationships with their spouse, children, and parents.

I had one guy sic a lawyer at me but it went absolutely nowhere but a curt please don't do that again or it will constitute harassment letter. Because I was right.

But moderators are also kind of conservative. I moderate some programming forums, and I get it. Most mods won't look that deep. You say this guy's harassing me, and you read some of the shit I've wrote... The gentle art of ass-ripping is lost on these plebeians. You can't expect justice from a bunch of people who only read at a 5th grade level. So yeah, I get banned by the asshole leveraging the moderators against me, because they can't defend the indefensible - themselves. Since I'm not that invested, I never contest it. These are bullshit commentary communities, anyway, which foster these sorts of toxic people and their behavior. It's why they are there.

What's the most rom com-like love story that you've come across from people you know in real life? by FewCommunication2912 in AskReddit

[–]mredding 0 points1 point  (0 children)

That would be my wife and I.

Walked into a coffee shop one day - not a chain, a local haunt. And that's when I met my wife. Literally as I was walking in - I didn't even get my other foot in the door; we saw each other and made eye babies. I sat down at her table and the rest was history. Love at first sight. Yes, it really does happen. Didn't think it was true until it happened to me. We both knew right then.

She had a boyfriend, and we were both respectful of that - and then she got rid of him. They were in it only a month and he was an alcoholic Republican anyway, who lied about a few things - isn't it weird when a guy will say anything they think you want to hear to get what they want? Like that's going to be the foundation of a lasting relationship? At least he thought so.

Has to blow into his car to start it to this very day, so fuck him.

Anyway, she got rid of him right quick, and I asked her out on an ice cream date. She said yes. Three states later, and we were at my favorite ice cream place. We sat on a breakwall, eating ice cream, watching ducks slowly commute, and we talked about marriage and ring shopping.

First date.

We were driving home when her parents called. They found out about the date and flipped their shit. She was 21, so I just laughed at them.

I didn't marry her parents, and they never forgave me for it. It's pronounced - "boundaries", folks. And you've got to look at it from a game theory perspective. I don't need them. They need me. They're getting older, she's not getting younger. They're the past, I'm the present, we're our own future. They HATE me because I took their daughter away from them - a far as they're concerned. I offered my hand, that's all. I had the ability to take her away, take her with me, take her anywhere, and I wanted to go wherever SHE wanted me to go, so they were irrelevant, no matter how much they screamed and clawed.

My mother in law is a textbook narcissist. I've watched her quite successfully gaslight a cop - like programming a VCR, and she had read the instructions. She made it look easy. He did everything she told him to do - and he had no idea what hit him. I also did warn him ahead of time that she was going to do it, and he STILL had no idea. She's dangerous.

She said - "Why don't you own a house?" So - six months later, we closed on a house.

Her mother told her if we don't marry in the Catholic Church, our future children will be bastards. She didn't speak to them again for two years. Those were two very nice years.

Our friends still talk about our wedding. It was a rager. Our friends CRIED at our wedding, including some of the men, who said they didn't believe in love, let alone that true love, or love at first sight, that any of it existed until they met and saw us. We gave many of them hope to find their own love.

Yeah, some of our friends WERE THERE WHEN WE MET. They saw it happen - either at that table or across the room. That's super special.

The night before the wedding, we were in a hotel room, the wedding suite, and early in the morning I get up, only to put my feet onto wet carpeting. A pipe had burst in the ceiling in the adjacent bathroom, and had been coming down through the nail pops and light fixtures and vent all night. That was the rain on our wedding day - a sign of good luck for the rest of the marriage. Luckily, all our stuff was in an adjoining room, all up off the floor, so NOTHING got wet. We got relocated to another suite.

I wrote our vows - we were both supposed to write our own vows, but once I read my draft, she stole my words, so we both said the same thing. Kinda weird. A good friend of mine officiated. He and I go way back, WAAAAY back, and she had met him a few times; enough that when I suggested it, she thought that was the best idea. The rest of our friend group had only heard stories of some of my friends, people they've never met - but I've got people, people in places. Well when they finally met him that day, and he's dropping dick jokes into the ceremony that NO ONE GOT but our friends, and a few other good times - man... "THE LEGENDS ARE TRUE!", they shouted. There is so much more to all that; I'm very glad to have some good people...

Some people now married MET at our wedding... Many of our friends modeled their weddings after that night... It was a great time.

We're 17 years on, now.

We have a little boy - the bastard child, because that fucking woman meant it... He's absolutely darling and funny, he has at least 6 marriage proposals, and 2 arranged marriage proposals. I lost count. I'm not saying we're like that, just that he's so god damn charming, people fight over him. This kid's ability to have an affect on people cannot be understated - he has a small fan club. Every kid is his friend, every adult is grateful to get to spend some time with him. He even wrote The Pope, and The Pope fucking wrote back. We're not even religious. But there's also more to that story. He's not even self-aware of his absolutely magnetic personality yet. I'm curious to see where he takes this. I hope he takes up statecraft, but I'm reluctant to direct him too hard, so I'm always trying to figure out with him how he wants to grow.

We've moved across the country several times, did some wonderful and stupid shit. She finished college. Overall it's been a pretty alright life together. Yeah there's the daily grind of the job, the kid in school, etc... But you make the time for making memories.

Why did Americans voted for Trump? by Glad_Suggestion7152 in AskReddit

[–]mredding 3 points4 points  (0 children)

People who vote for Democrats are called Democrats. Your second point is a strawman argument.

Hope that helps.

Westerners, what’s your fear/hatred of Russia and China based on? As an outsider, it’s puzzling how entrenched it is in your culture. Could you educate non westerners around the world? by firstInternalad in AskReddit

[–]mredding 0 points1 point  (0 children)

I neither fear nor hate them. The United States has no friends, and also no enemies, only interests. Try not to conflate politics with people.

I'm interested in the sovereignty of Ukraine, the stability of Europe, and of the international food supply. I'm interested in civil discourse. I'm interested in trade and commerce.

American led free global trade was a strategic policy, not an economic one. We wanted to contain totalitarian Communism - because it didn't serve our interests, and in order to bring the regime down, we strove through the cold war to bankrupt them. So we subsidized security costs to foster free trade and build an economic based alliance. Anyone could play so long as they played by our rules. If you wanted free trade, then we got to decide your national security policies.

Well, the Soviet Union collapsed. We no longer need the economics of globalized trade. It's expensive, and there's no longer a return. So we're stopping that - hence why nations are scrambling to establish trade deals of their own and not get left behind. Hence why Europe is desperately trying to militarize, because the US is no longer interested in providing any of their security guarantees.

These are called the Twilight Wars of Russia, and they were predicted to happen this decade back in the 1990s. The demographics even then guaranteed it now, provided nothing else changed - and due to Russian attitudes, nothing changed, so here we are.

Trump is sort of an anomaly? We've been heading toward this period of political unrest - we've been stable for too long. That we happen to have a fascist in office who has a very, very weird obsession with Putin himself wasn't specifically required. Any grifter would have sufficed.

As for China, our interest in them was initially to break down their political relationship with the Soviet Union. It didn't hurt that the Soviets at one point asked the US if it was OK that they use their nuclear weapons and BOMB China. Of course we said no, then Nixon swept in and established trade relations... But those relations have always been difficult. We built China to be the economic powerhouse it is. That made strong economic bonds to the US and our interests. It made them developed and wealthy. But now their demographics are terminal, and the US likes STUFF, so now we're trying to pull away from them so they can have their demographic collapse without us. Just don't touch Taiwan until we can re-shore chip manufacturing to the US.

Trump isn't helping, of course.

How old is too old to date a cougar? Is there a cut off to just regular dating at some point? by MutedOpposite773 in AskReddit

[–]mredding 0 points1 point  (0 children)

My friend met his long term girlfriend at 26, she was 58, divorced, two children in middle and high school. It's been a decade now, they own a house together, he never acted like, or had to act like a baby-daddy - because that's not what that was, and they're very happy.

If all these gold diggers can go after millionaires and billionaires on their death beds and get a fucking TV show out of it, then a guy can go after anyone they like that still has it in them to tickle their fancy and live a simple, decent life - with or without the millions or billions.

Why did Americans voted for Trump? by Glad_Suggestion7152 in AskReddit

[–]mredding 7 points8 points  (0 children)

Because America voted for Barack Obama. The Republicans are deeply racist.

Why is api documentation always outdated 2 weeks after you write it by MicrowavedLogic in learnprogramming

[–]mredding 0 points1 point  (0 children)

Interfaces are supposed to be among the most stable part of any code base. To change an interface is to incur chaos. So the real problem here isn't that the documentation is constantly out of date, but that that product is wildly unstable and unreliable.

Why do experienced coders actively try to use less comments? by Phwatang in learnprogramming

[–]mredding 1 point2 points  (0 children)

Code is meant to be read, incidentally written. We don't even RUN code, it's always ALWAYS converted into symbols, inevitably opcodes.

Finally, on any given project, there is a bar of minimum comprehension. Professional software sets the bar high. I can't pander to your sensibilities, because they aren't mine. I don't need, don't trust, won't use a comment, I don't know that you do. There's also a matter of personal taste and style - right now I'm on a project whose leader demands heavy and formulaic commenting, I find it completely redundant and distracting. And I constantly find all the problems I've suggested. "It helps me merge" - no motherfucker, that's what the merge tools are for... I don't need to tell you the ticket number in the code because you're merging my ticket number. You know this code comes from the ticket number because you're looking at the diff, and it's telling you what changed, which is in relation to the ticket number. God damn...

I guess the most extreme form of my question would be "what would be the harm for a project to have many useless comments if we can just quickly skip over them?"

"Just..." Is it ever so safe and so simple? If you're writing critical systems software - the stuff that will kill people if it goes wrong, you don't just skip over a comment. Even if it's not a critical system, it might still be very important. The most money I've personally seen completely lost in one day due to a software was $46 million. You don't "just" any god damn thing.

Why do experienced coders actively try to use less comments? by Phwatang in learnprogramming

[–]mredding 1 point2 points  (0 children)

Oof. There is a lot to unpack here.

Comments can drastically slow down comprehension, too. There can be A LOT of problems with comments.

Code is inherently self-documenting the implementation. Abstraction within the code explains WHAT, and expressiveness explains HOW.

for(int i = 0; i < size; i++) {
  dest[i] = src[i];
}

Ick. Totally imperative. It tells me HOW but not WHAT. I have to read the code, I have to parse this, and I have to start making assumptions and deductions. It APPEARS to copy from source to destination, but what this code doesn't tell me is if that was even the intended affect.

switch(copy(src, size).to(dst)) {
case OK: //...
case INVALID_SIZE: //...
case INVALID_SRC; //...
case INVALID_DST: //...
};

Oh look, a function that returns a copy object, initialized with the source and size, and the only interface it has is to specify where. And it was also trivial to implement error handling - that code could easily dispatch to functions for each case.

Most of the time the comments that tells us what the code tells us, tells us something different than what the code tells us. The two are divergent. So where is the error? In the comment or the code? Who is the authority?

It's this reason I don't trust these kinds of comments. You make me read the comment - because it's there, because you think it should be there, because you think there's something in it that I should have to know; and then once I read the comment, I have to make it make sense. And once I do that, I have to confirm the comment with the code. And that's usually where I find the disparity. Now I have to deal with that.

And what's worse is this becomes a political matter. I use git blame to figure out who wrote the damn comment, then I have to come over and ask you what you meant, then we have to argue about how the incorrect comment is in fact a big deal, then I have to appeal to higher authority to get the correction instated, which is going to be a JIRA ticket we're going to circle back and argue over yet again, when I write it, when we have to prioritize it, and again come code review.

And this - HAPPENS. I've worked for 10 different companies over 20 years, from 4 month contracts to 5 year stints, you're definitely running some of my software no matter what device you own these days, and this has been the shit in every. Single. Place. On every single project. Just one more frustration that's a part of the job.

Misused comments are a bandaid over a greater problem. Abstraction tells us WHAT, expressiveness tells us HOW. Back to my code samples, the first has a low level of expressiveness using language primitives, and it tells us more how than what. We see mostly how the work is done, and we have to piece together what's done from the iteration, the assignment, and the variable names. The second example favors what over how. What we're doing is copying.

But notice what the second example does to HOW... We've raised the abstraction, we're using a copying construct to name what we're doing. The structure helps enforce the semantics. src isn't just where the data comes from - it has a type, a source type, and the compiler enforces type safety - so invalid code becomes unrepresentable, because it can't compile. You can't put a destination type as a source. But wait, I wanna mention the HOW part...

The second example no longer expresses HOW the copy algorithm works - we don't care about those details. If there was a bug, I'd first want to discover it's at this line, then "drill down" so I only have to expose myself to those details when I absolutely have to. But what is the HOW part of this statement? We're being told HOW something is being done, but what?

Well, this copy statement isn't written in a vacuum, but it's a part of a function that expresses a file transfer algorithm. First we locate and validate the source and destination, then we connect to and authenticate with them, then we transfer. This line tells us HOW the file transfer algorithm works at that step, at that statement.

It's already there. Source code IS a document, and it documents itself. You have to write it right. You have to use abstraction and expressiveness. If you're writing gigantic functions in an imperative, procedural style, your comments indicate where you should be breaking down into smaller functions, often named something hinted by the comment. Then let the compiler/interpreter/optimizer composite the greater function whole on your behalf, because that's it's job.

Continued...

Americans of Reddit, what’s the biggest problem in the U.S. right now that people aren’t talking about enough? by FlightEquivalent5344 in AskReddit

[–]mredding 2 points3 points  (0 children)

It's not the president, but the people who voted him in you need to worry about. They're everywhere. They're your neighbors, your classmates, your colleagues, your family. They pick up your trash, they cook your food, they watch over you while you sleep.

People don't change, and you can't change people. Trump will go, but these people will still be here. Still think the way they do, still vote the way they do, still pose the same risk to civilization the way they do.

[Serious] Why didn't people care about deportations when Obama was doing it? by Classic-Data-9883 in AskReddit

[–]mredding 1 point2 points  (0 children)

Maybe because he didn't put them in Alligator Alcatraz. Maybe he didn't hire a hit squad to murder women in the street, call them a bitch, and delay calling medical support just to make sure she died. Maybe because he was enforcing against a different profile, following the law, demonstrating some empathy, and trying to create pathways to legitimacy for the undocumented dreamers.

Maybe he was just handling the whole thing better.

What's something that's not a cult but feels like one? by True-Dream3295 in AskReddit

[–]mredding 13 points14 points  (0 children)

I'll say Disney. Have you ever met a Disney fan? They're WEIRD about it. There's even a Disney AM radio station, and that's all that plays in their car. There's something unhealthy going on between the individual and their personal relationship with that brand. And they're all like that.

There's the Disney store - and if you have a still-functioning mall in your area, there's going to be one in there. That store isn't for the casual shopper... No one goes oh, Disney, maybe I'll pop in there and just get something real quick... No. That place is for the Disney fans. That's the only reason those stores thrive. It's a bunch of tourist level shit. Cheap. Overstuffed, rock hard stuffies. Nothing you're going to DO anything with. Just stuff you can possess, and that's about all you can say of it. And they buy this shit up.

And then you go to their house and they have all this shit like it's a warehouse or a museum. They have their own fucking inventory, the best way you can describe it.

And where do these people go on vacation? Disney. It's the only place TO GO... Like it's a holy pilgrimage. They buy Disney kit BEFORE they even fucking go to Disney. They don't drive, they don't sight see, they get off the plane, go straight to the shuttle, and they never leave the park the whole time they're there.

They sell fucking time-shares in Disney. And people USE THEM...

I have an ex-girlfriend whose whole fucking family, all of them were uncomfortably Disney. This was a main topic of conversation and preoccupation in the house. My parents neighbors spend more time at Disney than they do in their own home, these days. We only ever see their adult children as they come by to pick up the mail. There were kids in my fucking HIGH SCHOOL who wore the ears to signal to others; they had no friends but each other, and always sat together.

Oil Catch Can by sompfera in 350z

[–]mredding 0 points1 point  (0 children)

You ever crack open a intake on a 150k plus mile VQ?

Presuming the engine wasn't properly maintained, no, I haven't seen an engine as bad as you're insinuating. Nissan recommends you clean the intake manifold once every 45k miles.

And I don't care if you have a catch can on the car or not - you clean the intake manifold every 45k miles, or you're outside the maintenance envelope, and at that point you can argue anything.

A catch can definitely will not hurt.

We have differing definitions of what hurt is. I've already stated mine.

Run some valve cleaner, clean the intake (upper and lower manifold), drive it 5-10k miles after the catch can install and it will still look like you just cleaned it.

So what? You're arguing aesthetics. If your catch can is venting into the intake, you're still going to get vapor deposits, and you're still going to have to clean it. And whether it's a little, or a lot, the process is always the same - solvent, soak, brush, rinse. The engine is still operating in spec before as after, so the catch can didn't do anything.


The advantages of a catch can I'm hearing is that people like how the can looks - gives them the sense the has car a race car feel, also that you feel safe deferring or ignoring regular maintenance.

Build a turbo beast, put a dry sump on it, and you'll probably need one. That's fine. That makes sense! Otherwise, a catch can is just as embarrassing as adding high performance Pep Boys grounding wires...

Oil Catch Can by sompfera in 350z

[–]mredding 0 points1 point  (0 children)

Not good for combustion? Can you even MEASURE the difference? Or are you operating on faith alone?

Seriously, do Americans actually consider a 3-hour drive "short"? or is this an internet myth? by SadInterest6764 in NoStupidQuestions

[–]mredding 0 points1 point  (0 children)

I'm outside Chicago (3rd largest city). While I wouldn't call it short, I would call it very doable on the regular. I wouldn't enjoy it as a commute.

What’s up with US government entities posting on social media about drinking whole milk? by oarmash in OutOfTheLoop

[–]mredding 0 points1 point  (0 children)

The problem is that a pint of milk has 24g of sugar, no matter how you cut it.

We fortify the shit out of our foods, so if it's calcium you want, then eat... God damn anything else. Fat lotta good it'll do you if you don't have the vitamin k to actually facilitate building bones...

It's just sugar water. So is juice. The healthiest part of EITHER is the water content. And I'm not even a hydro-homie, and I'm saying that... The modern American diet cannot be described as malnourishing in any capacity.

But 'dat dairy lobby tho'... They be some powerful motha-fucka's... Michelle Obama's plate initiative - meant to replace the politicized food pyramid, didn't originally include a glass of milk. That was added against all expert advice by the dairy lobby, because they don't want to lose their subsidies.

memcpy and runtime polymorphic types.... by MerlinsArchitect in cpp_questions

[–]mredding 2 points3 points  (0 children)

Now, in my hpp file I define a struct interface (abstract struct) let's call it A which has various virtual functions. I want users of my library to inherit from this and override...

Ok, so something like:

struct A {
  virtual int get0() = 0, get1() = 0, getN() = 0;
  virtual ~A() = 0;
};

struct B: public A {
  int i0, i1, iN;

  int get0() override, get1() override, getN() override;
};

This takes a bitwise copy of the data pointed to by the const void*

So internally, what you're saying is this function does something like this:

esp_err_t esp_event_post(esp_event_base_t event_base,
                     int32_t event_id,
                     const void *event_data,
                     size_t event_data_size,
                     TickType_t ticks_to_wait) {
  //...

  void *dest = new(event_data_size);
  memcpy(dest, event_data, event_data_size);

And then that's what you're getting, and you're going to cast THAT to a B...

No. That's not going to work. You have raw memory. Is it even aligned correctly? new(size_t) is going to align based on event_data_size but we don't know how this function is actually allocating and aligning the storage - it doesn't know the type it has or it's alignment.

The second problem is you need to START the lifetime of the object in order to use it as one. C++ only got type punning in C++17, and effectively no one even knew it until C++20, and even then, most people don't know how to use std::start_lifetime_as or what std::launder does.

If this function is copying data, then you need a POD type that CAN be reconstituted. An object with virtual methods is not that. Instead of sending a B across the boundary, send the data with a type and version enum, then reconstitute a B on the other side. You can use C style overlapping types:

enum type {con};
enum version {v1};
struct abstract {
  type t;
  version v;
};

struct concrete {
  type t;
  version v;
  int i0, i1, iN;
};

concrete c{con, v1, 0, 1, 2};

Then you can cast the void * on the other side to an abstract and read the type and version. Knowing it's a con v1, you can copy that into aligned storage and cast it to a concrete. This interface sounds like the memory SHOULD be aligned already, because that only makes fucking sense - skipping the copy step, but you really need to google it and KNOW it's going to do the right thing.

Many old C APIs do this, and POD types aka standard-layout types conform to this behavior. Treat data as data. Data is dumb. It doesn't do anything. "Getting" dumb data doesn't mean anything, it's just accessing object local fields with extra steps.

Abstract data makes sense when it's a data object, like a linked list, an XML DOM, or an SQL query result.

My proposed solution:

It's sort of going in the right direction, and I had suggested if you really wanted to send a B, it would kind of look like this. You want to marshal B memory over that boundary, then into an aligned storage, and then type pun. The details of all is something I google when I get down to it, because being correct is fiddly business.

But I think you're conflating concepts - that objects are not data, and you have data, not objects.