<< >>
justin = { main feed , music , code , askjf , pubkey };
[ <<< last (older) article | view in index | next (newer) article >>> ]

August 2, 2024
Things I like about C++: operator overloading

When I first started programming C++ in the 1990s, there were language features that I found appalling. One of those was operator overloading, which was used in the most basic of C++ example code, e.g.

  cout << "hello, world" << endl;

This made the C programmer in me squirm. Why would you make the meaning of operators change wildly based on the context? Also you might lose track of what code is actually being generated. It could have side effects that you don't know about, or be orders of magnitude slower! I still fill this way, and avoid operator overloading, other than operator=, for most things.

...but having said that, I find operator overloading to be incredibly valuable when it comes to maintaining and refactoring a large code base. A pattern that has become routine for us is a basic type is initially used for some state and needs to be extended.

For example: in track groups, we originally had 32 groups (1-32), and a track could have different types of membership in any number of groups. For 32 groups, we used unsigned ints as bitmasks. Some years later, to support 64 groups we changed it to WDL_UINT64 (aka uint64_t). Straightforward (but actually quite tedious and in hindsight we should've skipped the type change and gone right to the next step). To increase beyond 64 bits, there's no longer a basic type that can be used. So instead:

  struct group_membership {
    enum { SZ = 2 };
    WDL_UINT64 m_data[SZ];

    const group_membership operator & (const group_membership &o) const
    {
      group_membership r;
      for (int i = 0; i < SZ; i ++) r.m_data[i] = m_data[i] & o.m_data[i];
      return r;
    }
    // and a bunch of other operators, a couple of functions to clear/set common masks, etc

  private:
     // prevent direct cast to int/int64/etc. necessary because we allow casting to bool below which would otherwise be quietly promoted to int
    operator int() const { return 0; }
  public:
    operator bool() const { for (int i = 0; i < SZ; i ++) if (m_data[i]) return true; return false; }
  };

Then we replace things like:

  WDL_UINT64 group;
with
  group_membership group;

and after that, (assuming you get all of the necessary operators mentioned in the comment above) most code just works without modification, e.g.:

  if (group & group_mask) { /* ... */ }

To be fair, we could easy tweak all of the code that the operator overloading implements to use functions, and not do this, but for me knowing that I'm not screwing up the logic in some subtle way is a big win. And if you have multiple branches and you're worried about merge conflicts, this avoids a lot of them.

Also, it's fun to look at the output of gcc or clang on these things. They end up producing pretty much optimal code, even when returning and copying structs. Though you should be sure to keep the struct in question as plain-old-data (ideally no constructor, or a trivial constructor, and no destructor).

Thus concludes my "a thing I appreciate about C++" talk. Next week, templates.

Recordings:

super8_novideo - 1 -- [9:36]
super8_novideo - 2 -- [4:16]
super8_novideo - 3 -- [4:34]
super8_novideo - 4 -- [8:20]






12 Comments:
Posted by wasereb4 on Thu 22 Aug 2024 at 19:51 from 95.90.106.x
i ++ - first time I've seen it written like this (with a space, rather than i++), why?


Posted by Justin on Sat 24 Aug 2024 at 14:10 from 71.125.233.x
why not? doesn't really matter much, very readable either way


Posted by Kiernan Holland on Thu 05 Sep 2024 at 12:05 from 47.185.56.x
Im a college friend of brennans.. Just hunting around for him. I am messing around with UDIO as well. I'm @udiorockmeamadeus on youtube. This is my mtv simulation website RockMeAmade.us . I was wondering if I could use some of your music as input via remix into UDIO, and then take the results of that and post with a CC:PDMk1.0 license like my other UDIO generations. I tell people I don't make music really, its more like catching butterflies or fibding oretty rocks. Im not going to fix my typos, thats proof im not ai, as ai would give you the most likely stuff. I hope Brenns still alive..I havent seen him in two decades, I just know he is your brother..


Posted by Kiernan Holland on Thu 05 Sep 2024 at 12:11 from 47.185.56.x
oops i post my music to archive.org and put it into public domain. Tell bren, I got some good luck.. Mt father had a thing in his pension program that permits me 3k/mo as long as im ungainfully employed and unmarried.. So I just spend my time fapping off and giving it back to humanity.


Posted by same guy on Thu 05 Sep 2024 at 12:24 from 47.185.56.x
You know youtube cannot put ads on their embeds, cause adsvertisers need a predictable context (ex. trump ad on biden site) and its only on the embed you can set the beginning and ending seconds on each video, if you could do that on youtube's playlists, people could remove credits for videos from playlists.. I'm pretty sure it still can't be done in the playlists on youtube.

So I created a website to take advantage of this and create a simple perpetual feed of video content, so that content is shown without repeats and thus equiprobable. A democracy that works, yay. All my channels of content at feed.org. . I keep a link to github to the last tar file of the entire site, but I'm too lazy to update it. Besides all the useful code is in the clientside javascript.. Roll your own..


Posted by oops on Thu 05 Sep 2024 at 12:40 from 47.185.56.x
feed.dog is all my playlists .. If a video is missing, come back in ten minutes, its all time syncd. I could force the embed to skip the missing video, but then the stream would be out of sync.. I want it to be a shareable experience.

There can be no repeat videos as the index to the playlist internally , is an associative array of the 11 byte youtube video id. I use bookmarklets to extract the videoids from pages when populating a playlist.. Its all hand coded in php, I'd use a framework, but I'm not that organized.. I rarely adjust or update the code. Its costing me $300 a year to keep the site running at godaddy. I once told someone I was so bored, I'd rather be watching my website. The exclaimed "I¡ve never heard of anything so narcissistic in all my life.. Your brothers last name is U... and he made a tank game in opengl on the sgis in a day, to pass the class, with an alice and chains song playing in the background.


Posted by kjh on Thu 05 Sep 2024 at 12:44 from 47.185.56.x
I tried to use html link the feed.dog, but it didn't work.. Iuse to plat planetside2, and found out the chat system used html, and found Zi could change the font size. Making the chat useless.. It forced daybreak to filter out the html .. It was fun while it lasted..


Posted by last one on Thu 05 Sep 2024 at 12:53 from 47.185.56.x
I don't play planetside2 anymore, since rogue banned my ps5's machine id..Im not sure, but I think thats what happened. I'd been banned more tgan anyone from the playstation version.. But I've spent at least 6k on that game.. Some of the freek user content is a burlap texture for vehicles.. I had to tell the people there determined not to pay for content, that rogue thinks they are cheap asses, why a burlap texture? Welll if you like porn, many of the vr porn services are offering lifetime memberships. Vrporn is good, slr sux except a stripper studio. I have lifetime memberships to slr and vrporn.. Czechvr is the best, but they dont have a lifetime membership.. Can you believe zuckerberg blew 40 billion on tge metaverse.. Brennan could gave had that thing working in a day..


Posted by okay one more on Thu 05 Sep 2024 at 13:08 from 47.185.56.x
THE QUEST 3 rulez..mAnd its suitable for UDIO authoring. Gemini told me it runs somewhere in soeed between a Apple M1 and M2.. Microsoft destroyed AltspaceVR, jerks, it was a great vr community. They started by removing all the alien avatars, so then realworld race became an issue cause you couldn't be a nonhuman.. Then they got rid of full finger articulation so those with xbox motion bar hacks could not use them... Then the for ed everything to Unity, which cannot run on a oculus go without causing it to overheat. I sideloaded minecraft onto my Gos and 60 monsters in a room won't cause a go to overheat, but find me a unity app running on a go that doesn't cause the go to overheat. I skipped the quest 2 from the quest 1, cause the 1 has two seoarate OLED dusplays thus variable ipd, quest 2 had fixed ipd, and is backlit lcd.. Q3 is same, but .. okay gotta make some chiptunes on udio, its what i do best..


your older tgan youve ever been, and now your even old, and now your even older...


You need a vr180 cam,, to bad you cant easily make steregram images of birds.. It doable, put cameras on drones or rc cars..In mt 30s (2003-2008) I used olympus cameras D380 and C750 to make stereograms of everything. The C750 has a supermacro mode with a focal distance of like 3/8ths of an inch.. So I would make stereograms of snowflakes.. It has a 2 in 1 mode tgat can be used to test the stereograms by layering the omages with my eyes.. If you caput a camera on your shoulder as you are riding your bike, you could collect a 3D stereogram of the


Posted by UDIO music TV channel , enjoy.. on Thu 05 Sep 2024 at 13:16 from 47.185.56.x
rockmeamade.us/mtv_play2.php?code=...


Posted by yes on Thu 05 Sep 2024 at 13:43 from 47.185.56.x
Udio version of beatles revolution number 9

youtu.be/PDDK5Gyzvro

The lyrics: number 9 number 9 number 9 ....


Posted by Justin on Fri 06 Sep 2024 at 21:47 from 71.125.233.x
lol hah I will pass on your greets, thanks for the reading material


Add comment:
Name:
Human?: (no or yes, patented anti crap stuff here)
Comment:
search : rss : recent comments : Copyright © 2024 Justin Frankel