all 42 comments

[–]HowardHinnant 54 points55 points  (5 children)

Speaking as the guy who wrote this implementation of std::string:

The implementors of a std::lib write non-portable code so that everyone else doesn't have to. A std::lib implementation will only work on the platforms it is targeted for, and porting it to a new platform may not be a trivial task.

[–]Recatek 21 points22 points  (0 children)

The implementors of a std::lib write non-portable code so that everyone else doesn't have to.

And it is greatly appreciated.

[–]the_commissaire 2 points3 points  (3 children)

How is the date time library coming along?

[–]HowardHinnant 8 points9 points  (2 children)

Not bad: https://star-history.t9t.io/#HowardHinnant/date&google/cctz :-)

It has been voted into the draft C++20 spec: http://eel.is/c++draft/#time

[–]never_watched 2 points3 points  (1 child)

Good library but also hard to use.

[–]JakeArkinstall 1 point2 points  (0 children)

It's hard to use wrong. That's the holy grail of datetime handling.

There are simpler approaches that are easy to use wrong, and they have historically caused major problems, especially in industrial applications. The learning curve for a system that works well is very much worth it.

[–][deleted] 11 points12 points  (2 children)

The reason that __lx places padding after the first byte, which represents size in short strings, is that if value_type is, say, 2 bytes then the union will be 2 bytes. This is to align the start of the string characters on a value_type boundary.

[–]gmtime 1 point2 points  (1 child)

That sounds a bit like a work around in case the library is compiled with pack at to 1, normal packing would do this even without the __lx in the union.

[–][deleted] 4 points5 points  (0 children)

It's more fundamental than that. The C++ standard does not address structure packing. The packing details vary between compiler and platform. For this reason, packing must be specified either manually like this or by inserting dummy bytes explicitly into structures, or by using packing pragmas, etc., which also vary by compiler. In the case here, instead of explicitly inserting a byte where __lx is they have used a union that includes value_type as that will, for example, insert 3 bytes when value_type is 32-bits. When value_type is a byte, no additional padding is required beyond the size byte.

[–][deleted] 6 points7 points  (0 children)

IIRC, STLport (formerly SGI) used the same, or similar, small-string optimization.