veganism.social is one of the many independent Mastodon servers you can use to participate in the fediverse.
Veganism Social is a welcoming space on the internet for vegans to connect and engage with the broader decentralized social media community.

Administered by:

Server stats:

293
active users

#tuesdaycodingtips

0 posts0 participants0 posts today
Jakub Neruda<p>Tip 89 of <a href="https://techhub.social/tags/TuesdayCodingTips" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>TuesdayCodingTips</span></a> - Semantic versioning</p><p>When creating a library, semantic versioning is a really useful versioning scheme to use.</p><p>It's a promise to your users that changes bumping the minor/patch version numbers are backward-compatible. As such, much of the software relies on these numbers to figure out whether a particular version requirement can be satisfied by higher-version binaries.</p><p>For example, Unix SONAME links provide a stable way to (typically) reference a major version of a library, while the pointed-to file can be updated to newer revisions.</p><p>If you only specify the major (or major.minor) version number, CMake Find* scripts will find the newest compatible package. Various wildcards in cargo, npm, and other package managers follow the same principles.</p><p>Just be sure to read the full FAQ at <a href="http://semver.org" rel="nofollow noopener" translate="no" target="_blank"><span class="invisible">http://</span><span class="">semver.org</span><span class="invisible"></span></a> and know that adhering to this versioning scheme is hard, but your users will love you for it... (1/2)</p><p><a href="https://techhub.social/tags/tips" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>tips</span></a> <a href="https://techhub.social/tags/programming" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>programming</span></a> <a href="https://techhub.social/tags/semver" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>semver</span></a></p>
Jakub Neruda<p>Tip 88 of <a href="https://techhub.social/tags/TuesdayCodingTips" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>TuesdayCodingTips</span></a> - Enum-discriminated unions with nlohmann::json</p><p>JSON is a fairly trivial format and as such, its only way of supporting polymorphic data is an enum-discriminated approach.</p><p>In other words, you might have an array of objects where each object has a 'type' property and all other properties are determined by the value of the type. Circle might have radius, rectangle has dimensions, both have position.</p><p>But how would you parse that in C++ with `nlohmann::json` and a minimal boilerplate? While the library doesn't have a native support for `std::variant`, let alone recognizing this pattern, it allows you to extend it easily with a custom `adl_serializer` specialization.</p><p>Remaining objects can still be handled by appropriate macros provided by the library to reduce further boilerplate.</p><p>Compiler explorer link: <a href="https://godbolt.org/z/KsW4dhj5j" rel="nofollow noopener" translate="no" target="_blank"><span class="invisible">https://</span><span class="">godbolt.org/z/KsW4dhj5j</span><span class="invisible"></span></a></p><p><a href="https://techhub.social/tags/cpp" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>cpp</span></a> <a href="https://techhub.social/tags/tips" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>tips</span></a> <a href="https://techhub.social/tags/programming" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>programming</span></a></p>
Jakub Neruda<p>Tip 78 of <a href="https://techhub.social/tags/TuesdayCodingTips" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>TuesdayCodingTips</span></a> - Many ways to read a file in C++</p><p>In C#, you can easily read a whole file using `File.ReadAllText` method. Obviously, things can't be as easy in C++.</p><p>You can choose the 'old way' where you pre-allocate a buffer to which you can read the data. This process involves seeking the end of the file and figuring out how big it is. I dare you writing this version from memory first time without an error.</p><p>Then there is the 'iterator' way. Just convert the stream object into an iterator and use that to initialize the string. Short, but not very memorable (not for me at least).</p><p>The easiest to remember (arguably) approach is to convert the file stream into a string stream and then easily extract the string contents from there.</p><p><a href="https://techhub.social/tags/cpp" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>cpp</span></a> <a href="https://techhub.social/tags/programming" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>programming</span></a> <a href="https://techhub.social/tags/tips" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>tips</span></a></p>