C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Achieving pragma pack(1) with typedef<unaligned>

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Sun, 7 Jun 2026 23:57:46 +0200
For your second suggestion:     typedef< std::optional > J K;   You want to use typederf<> as metaprogramming facility, which applies std::optional (or whatever template you use as a parameter) to each of the member variables?     While this is a powerful type theoretical feature, if you want to do something like it, I want to be able to convert arbitrary types into arbitrary types and convert member functions, too.   Your feature is limited to converting all member variables to a certain template class.     Too powerful to focus on one problem, not powerful enough to be really interesting.     -----Ursprüngliche Nachricht----- Von:Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]> Gesendet:So 07.06.2026 22:50 Betreff:[std-proposals] Achieving pragma pack(1) with typedef<unaligned> An:std-proposals <std-proposals_at_[hidden]>; CC:Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>; Most people here will be familiar with:    #pragma pack(1)    struct J {       int  a;       char b;       int  c;       char d;       int  e;    };    #pragma pack() On most computers, this brings sizeof(J) down from 20 bytes to 14 bytes. I have an idea about how to standardise this. Firstly we need a new template class, "std::unaligned", which you can see defined here:    https://github.com/healytpk/gcc-thomas-healy/blob/typedefangles/libstdc%2B%2B-v3/include/std/unaligned So the aforementioned struct could then be written as:    struct J {       unaligned<int > a;       unaligned<char> b;       unaligned<int > c;       unaligned<char> d;       unaligned<int > e;    }; But the above is a little tedious to write. That's why I suggest a new form of 'typedef' that's followed by angle brackets, and you can use it as follows:    struct J {      int  a;      char b;      int  c;      char d;      int  e;    };    typedef< std::unaligned > J K; As you guessed, the struct K will be the struct J but with every member variable type enclosed in unaligned<>. You can put any class template you want in there, for example:    typedef< std::optional > J K; Try out here up on GodBolt:    https://godbolt.org/z/a8zoYavYE Personally I think this is the No. 1 best strategy proposed so far to add packed structs to standard C++. -- Std-Proposals mailing list Std-Proposals_at_[hidden] https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2026-06-07 22:01:05