Hacker Newsnew | past | comments | ask | show | jobs | submit | david2ndaccount's commentslogin

If you want to remain portable, write your code in the intersection of the big 3 - GCC, Clang and MSVC - and you’ll be good enough. Other implementations will either be weird enough that many things you’d expect to work won’t or are forced to copy what those 3 do anyway.


This is what I have been doing for years. Works well for me.

Sometimes it is annoying but realistically it is a good strategy.


You can apply `#` to __VA_ARGS__, which won’t preserve the exact whitespace, but for many languages it’s good enough. biggest issue is you can’t have `#` in the text.


I wrote a similar article in the past: https://www.davidpriver.com/ctemplates.html

I use this technique in my hobby projects as it has worked out the best for me of the options I’ve explored.


Works with MSVC if you add /Zc:preprocessor (to get a standard compliant preprocessor instead of the legacy one).


The author relies on the compiler fitting the bitfield in unused padding bytes after “speed”, but that is implementation-defined behavior (almost everything about bitfields is implementation defined). MSVC will align the unsigned bitfield to alignof(unsigned), whereas GCC and clang will use the padding bytes. To portably pack the struct, use unsigned integers and use flags (monster.flags & CAN_FLY instead of monster.can_fly).

See https://c.godbolt.org/z/K7oY77KGj


Bitfields have other problems. Say you have two bitfields each of `uint8_t` type and totaling 16 bits: well, you might think that's _two_ fields, but the compiler is allowed to treat them as _one_ whenever it wants to, and _that_ can be a problem if you're accessing one with atomics and the other without because the other can yield unsynchronized accesses to the one that needs synchronization.

Bitfields in C leave way too much behavior to the compiler or undefined. It's really intolerable.


Even worse: bit fields can only be applied to int, signed int and unsigned int (maybe char as well but i dont remember)

Even crazier is the fact that an int bitfield's signedness is implementation defined


> Even crazier is the fact that an int bitfield's signedness is implementation defined

Easy fix: just make them always unsigned. But the other problems are much more serious.


Why can't there be a standard defined for bitfields in future C releases? This is a long-discussed drawback of a feature that I really really want to be able to use in production code.


There is, but it's part of the platform ABI and not the C language standard. The latter specifies syntax and behavior, the former is what's concerned with interoperability details like memory layout.

I happen to have a copy of AAPCS64 in front of me, and you can find the specification of bit packing in section 10.1.8. The sysv ABI for x86/x86_64 has its own wording (but I'm pretty sure is compatible). MSVC does something else I believe, etc...


There is. I'm not sure when it was added, but looking at https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf, page 106, note 13

> An implementation may allocate any addressable storage unit large enough to hold a bit-field. If enough space remains, a bit-field that immediately follows another bit-field in a structure shall be packed into adjacent bits of the same unit. If insufficient space remains, whether a bit-field that does not fit is put into the next unit or overlaps adjacent units is implementation-defined. The order of allocation of bit-fields within a unit (high-order to low-order or low-order to high-order) is implementation-defined. The alignment of the addressable storage unit is unspecified.

In practice, I've been using this feature for ages (along with __attribute__((packed))). There comes a point where you can start depending on compiler specific features instead of relying solely on the standard.


The portion you quoted derives word-for-word from the original ANSI standard (C89/C90): https://port70.net/~nsz/c/c89/c89-draft.html#3.5.2.1


Can this be standardized? It seems it would have accommodate the most limited architecture, in terms of memory access, probably making it practically useless. Seems best left to the compiler.


The quoted text is from the C standard. The poplar wisdom is that anything goes wrt bit-fields, but it's an exaggeration.

The original (?) DNS library used bit-fields to read and write the packet header status and flags fields, and that original code is still used today: https://github.com/openbsd/src/blob/master/include/arpa/name... It does rely on implementation-defined behavior--it swaps the order of fields based on whether the machine is little-, big-, or middle-endian--but even that behavior has remained remarkably consistent, at least on Unix ABIs.

I think the wisdom comes from dealing with niche compilers, especially from years ago, where bit-fields were one of the areas where standard adherence was less consistent; and in embedded and kernel contexts, where the implementation-defined and (implicit) undefined behavior, such as wrt atomicity or the intersection of bit-fields and (extension) packed pragmas, counseled against any usage of bit-fields.


I've been working on an open source library that, while it doesn't solve bitfields, can provide convenient ergonomic access to bit-granular types:

https://github.com/PeterCDMcLean/BitLib

You can inherit from bit_array and create a pseudo bitfield:

  class mybits_t : bit::bit_array<17> {
    public:
      // slice a 1 bit sized field
      // returns a proxy reference to the single bit
      auto field1() {
        return (*this)(0, 1);
      }

      // slice a 3 bit sized field 
      // returns a proxy reference to the 3 bits
      auto field2() {
        return (*this)(1, 4);
      } 
  };
  mybits_t mybits(7);
  assert(mybits.field1() == 1);
  mybits.field1() = 0;
  assert(static_cast<int>(mybits) == 6); //no implicit cast to integers, but explicit supported

There is minimal overhead depending on how aggressively the compiler inlines or optimizes*



different/more than std::bitset<SIZE> ?


Only similar by a little bit. Bitset misses the mark in so many ways.

bit_array can be compile time storage (ala bitset) or dynamic at construction time. There is also bit_vector which is entirely dynamic

Critically though, is that the type/library is built on a proxy iterator/pointer that lets you do certain things with STL or the bit algorithms that you cannot otherwise do with bitset.

But back to the bitfields idea. The bit array backing storage is configurable through templates so you will be able to know for certain exactly how much stack or heap your type will take up


C23 has _Float16


Summer reading programs are a band-aid on the problem that children shouldn’t have such a long summer break now that air conditioning is common. Spread the breaks out throughout the year if you want to maintain the same number of days off. All evidence shows the summer break is bad for children’s academic achievement (especially poor children), but it is viewed as a perk for the teachers so the teacher’s unions fight against questioning it.


Let’s say summer break is basically 3 months. I as a parent need to figure out childcare for that 3 month period at the beginning of summer. This is a much more time consuming endeavor than most would expect (or at least more than I expected). If you distribute those months throughout the year I need to repeat this process 3 different times, adding a bunch of overhead that could be spent on activities more beneficial to my family and kids.

Edit: Adding that I realize the summer slowdown absolutely exists and has a disproportionate effect on those that don’t need another wrench thrown in their life. But just wanted to add a perspective that isn’t “teacher union boogeyman”.


Not every school has air-conditioning however.

And there are schools that do year-round schedules, but the total time off is about the same. They will typically get a longer winter break, longer spring break, an additional fall break, and then a much shortened summer break, but those add up to about the same time off overall. I know many teachers who prefer that system, some because it means they get paychecks more consistently throughout the year, and also it gives you more spread out breaks and flexibility in taking trips instead of being locked in to summer/Christmas/one week in the spring.

The strongest push back to this schedule is in fact parents. The primary issue is once their kids are in different schools (high school / middle school / elementary) with different schedules this causes issues as kids are not longer on break at the same times. In addition summer camp programs are tied to the traditional schedule leaving kids in the year round schedule with fewer or no options.

In order to change it, you also need neighboring districts/communities/private schools/programming to all shift as well, otherwise it becomes too much of as hassle for parents & teachers.


Air conditioning is common, but at least in some regions, it would be a tremendous expense for the the school to condition their buildings for occupation during the summer. And many buildings were designed around the summer break, so they may not have capacity to condition the buildings for occupation during the summer; this is not without its problems as some buildings end up being unfit for occupation during the school year, especially as the climate gets less consistent. There's probably some opportunity for savings in places where increasing hours during the summer could result in decreasing hours in the winter, though.

I think there's some cultural value in having a shared experience of summer vacation. But I agree, breaking up the breaks throughout the year, where possible, would make a lot of sense. There's a benefit of less crowding when school districts have different weeks off; although it's harder for extended families to meet up when their school schedules are drastically different.


That assumes academic achievement should be the primary aim of childhood. What I learned in school was incredibly important—don’t get me wrong—but what I learned over the summer was arguably more important.

As a child of divorce, I cherished 6 straight weeks at my mom’s house (we only visited every other weekend during school). As a working class kid, I earned probably half my annual spending money over the summer.

My wife and I now have kids, and we’ve always loved to travel (and needed to just to visit family). Summer is the only time available for extended family trips (2+ weeks).


In other words, any time spent outside of school is time wasted?

We've cut the music and art in schools too. I guess the end state is one long endless math class. I'm sure those kids will be well adjusted.


It is the only vacation most teachers get, so of course they fight against shortening it


The argument wasn't for shortening it, but for distributing it through the year. I have never in my adult life taken 10 consecutive weeks off, and 5 two-week breaks would still be very generous.


Maybe summer break also has some value for the joy it brings to children? Their lives shouldn't just be preparation for adulthood, it's worth making childhood enjoyable too.


> We immediately began disclosure of this issue once we realized the potential impact. Unfortunately, no disclosure contacts were publicly available and we had to resort to emailing random people. The Paradox.ai security page just says that we do not have to worry about security!

Amazing.


Having a security.txt would be best, but they've updated the page to include a security email address which is a start.


One might even say paradoxical.


The “typeof on old compilers” section contains the code:

         (list)->payload = (item); /* just for type checking */\
That is not a no-op. That is overwriting the list head with your (item). Did you mean to wrap it in an `if(0)`?


In that example they also had replace the union with a struct - presumably to work around this issue. But that seems wasteful to me too. Doing it within an if(0) seems strictly better.


In D, you can just do a static foreach over a sequence to generate case labels:

https://d.godbolt.org/z/PxWEW14K1


Unfortunely like many things D, eventually C++ gets the feature, even if not as nice to use.

That is template for as part of the C++26 reflection work.

https://isocpp.org/files/papers/P2996R4.html

You will also find some well known names here,

https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p34...


I feel like the main problem people have with C++ is not the lack of features, but the absolute glut of slightly bad features. That's why I prefer D over C++ in any case: it's a much smaller language. You can learn enough to be dangerous in an afternoon, and enough to be proficient in a week or two.


I on the contrary, I rather reach out to C++, even though I like D, it isn't the features, it is the ecosystem.

Otherwise I rather stay in JVM/CLR/V8 land, when I don't need to.

I have been around D since Andrei Alexandrescu's book was published, even he is now back in C++ at NVidia, as his main work after he kind of stepped away from his role in D development.

And he is one of the figures on C++26 reflection papers.


Oh C++ has the clear advantage in libraries available, for sure. That's not really due to the languages themselves though I'd say. I'm honestly not quite sure why C++ got widely adopted and D did not.


Almost two decades predating it, and sadly no OS vendor picked up on it.

Many people forget C++ is a C sibling, born at AT&T on the same building UNIX and C were being handled, thus it was quite an easy win for C compiler vendors, to add C++ support to their toolchains.

Note that Objective-C also never made it outside NeXT, GNUStep was never that good clone, and had it not been for Apple's acquision and success, maybe we would no longer speak about it.

When Facebook or Remedy Games played with D, we hoped it would somehow improve adoption, that was never the case, and both companies no longer use D.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: