<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="FeedCreator 1.8" -->
<?xml-stylesheet href="http://wiki.nathancampos.me/lib/exe/css.php?s=feed" type="text/css"?>
<rdf:RDF
    xmlns="http://purl.org/rss/1.0/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
    xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel rdf:about="http://wiki.nathancampos.me/feed.php">
        <title>Nathan&#039;s KB - lang</title>
        <description>Hello</description>
        <link>http://wiki.nathancampos.me/</link>
        <image rdf:resource="http://wiki.nathancampos.me/lib/exe/fetch.php?media=wiki:dokuwiki.svg" />
       <dc:date>2026-05-13T09:02:39+00:00</dc:date>
        <items>
            <rdf:Seq>
                <rdf:li rdf:resource="http://wiki.nathancampos.me/doku.php?id=lang:cpp"/>
                <rdf:li rdf:resource="http://wiki.nathancampos.me/doku.php?id=lang:python"/>
            </rdf:Seq>
        </items>
    </channel>
    <image rdf:about="http://wiki.nathancampos.me/lib/exe/fetch.php?media=wiki:dokuwiki.svg">
        <title>Nathan's KB</title>
        <link>http://wiki.nathancampos.me/</link>
        <url>http://wiki.nathancampos.me/lib/exe/fetch.php?media=wiki:dokuwiki.svg</url>
    </image>
    <item rdf:about="http://wiki.nathancampos.me/doku.php?id=lang:cpp">
        <dc:format>text/html</dc:format>
        <dc:date>2024-10-12T17:44:07+00:00</dc:date>
        <dc:creator>Anonymous (anonymous@undisclosed.example.com)</dc:creator>
        <title>cpp</title>
        <link>http://wiki.nathancampos.me/doku.php?id=lang:cpp</link>
        <description>
&lt;h1 class=&quot;sectionedit1&quot; id=&quot;c&quot;&gt;C++&lt;/h1&gt;
&lt;div class=&quot;level1&quot;&gt;

&lt;p&gt;
It&amp;#039;s just like C except we can do more modern things with it. Also the newer versions of the language look like complete gibberish, avoid at all costs.
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT{&amp;quot;target&amp;quot;:&amp;quot;section&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;C++&amp;quot;,&amp;quot;hid&amp;quot;:&amp;quot;c&amp;quot;,&amp;quot;codeblockOffset&amp;quot;:0,&amp;quot;secid&amp;quot;:1,&amp;quot;range&amp;quot;:&amp;quot;1-172&amp;quot;} --&gt;
&lt;h2 class=&quot;sectionedit2&quot; id=&quot;casts&quot;&gt;Casts&lt;/h2&gt;
&lt;div class=&quot;level2&quot;&gt;

&lt;p&gt;
Casting in C++ can be quite confusing given the amount of possible ways to cast something. &lt;a href=&quot;https://stackoverflow.com/a/332086/126353&quot; class=&quot;urlextern&quot; title=&quot;https://stackoverflow.com/a/332086/126353&quot; rel=&quot;ugc nofollow&quot;&gt;This StackOverflow answer&lt;/a&gt; should contain all the information we need, although its contents as of 2024-10-12 have been replicated here:
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT{&amp;quot;target&amp;quot;:&amp;quot;section&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;Casts&amp;quot;,&amp;quot;hid&amp;quot;:&amp;quot;casts&amp;quot;,&amp;quot;codeblockOffset&amp;quot;:0,&amp;quot;secid&amp;quot;:2,&amp;quot;range&amp;quot;:&amp;quot;173-465&amp;quot;} --&gt;
&lt;h3 class=&quot;sectionedit3&quot; id=&quot;static_cast&quot;&gt;static_cast&lt;/h3&gt;
&lt;div class=&quot;level3&quot;&gt;

&lt;p&gt;
&lt;code&gt;static_cast&lt;/code&gt; is the first cast you should attempt to use. It does things like implicit conversions between types (such as &lt;code&gt;int&lt;/code&gt; to &lt;code&gt;float&lt;/code&gt;, or pointer to &lt;code&gt;void*&lt;/code&gt;), and it can also call explicit conversion functions (or implicit ones). In many cases, explicitly stating &lt;code&gt;static_cast&lt;/code&gt; isn&amp;#039;t necessary, but it&amp;#039;s important to note that the &lt;code&gt;T(something)&lt;/code&gt; syntax is equivalent to &lt;code&gt;(T)something&lt;/code&gt; and should be avoided (more on that later). A &lt;code&gt;T(something, something_else)&lt;/code&gt; is safe, however, and guaranteed to call the constructor.
&lt;/p&gt;

&lt;p&gt;
&lt;code&gt;static_cast&lt;/code&gt; can also cast through inheritance hierarchies. It is unnecessary when casting upwards (towards a base class), but when casting downwards it can be used as long as it doesn&amp;#039;t cast through &lt;code&gt;virtual&lt;/code&gt; inheritance. It does not do checking, however, and it is undefined behavior to &lt;code&gt;static_cast&lt;/code&gt; down a hierarchy to a type that isn&amp;#039;t actually the type of the object.
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT{&amp;quot;target&amp;quot;:&amp;quot;section&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;static_cast&amp;quot;,&amp;quot;hid&amp;quot;:&amp;quot;static_cast&amp;quot;,&amp;quot;codeblockOffset&amp;quot;:0,&amp;quot;secid&amp;quot;:3,&amp;quot;range&amp;quot;:&amp;quot;466-1413&amp;quot;} --&gt;
&lt;h3 class=&quot;sectionedit4&quot; id=&quot;const_cast&quot;&gt;const_cast&lt;/h3&gt;
&lt;div class=&quot;level3&quot;&gt;

&lt;p&gt;
&lt;code&gt;const_cast&lt;/code&gt; can be used to remove or add &lt;code&gt;const&lt;/code&gt; to a variable; no other C++ cast is capable of removing it (not even &lt;code&gt;reinterpret_cast&lt;/code&gt;). It is important to note that modifying a formerly &lt;code&gt;const&lt;/code&gt; value is only undefined if the original variable is &lt;code&gt;const&lt;/code&gt;; if you use it to take the &lt;code&gt;const&lt;/code&gt; off a reference to something that wasn&amp;#039;t declared with &lt;code&gt;const&lt;/code&gt;, it is safe. This can be useful when overloading member functions based on &lt;code&gt;const&lt;/code&gt;, for instance. It can also be used to add &lt;code&gt;const&lt;/code&gt; to an object, such as to call a member function overload.
&lt;/p&gt;

&lt;p&gt;
&lt;code&gt;const_cast&lt;/code&gt; also works similarly on &lt;code&gt;volatile&lt;/code&gt;, though that&amp;#039;s less common.
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT{&amp;quot;target&amp;quot;:&amp;quot;section&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;const_cast&amp;quot;,&amp;quot;hid&amp;quot;:&amp;quot;const_cast&amp;quot;,&amp;quot;codeblockOffset&amp;quot;:0,&amp;quot;secid&amp;quot;:4,&amp;quot;range&amp;quot;:&amp;quot;1414-2083&amp;quot;} --&gt;
&lt;h3 class=&quot;sectionedit5&quot; id=&quot;dynamic_cast&quot;&gt;dynamic_cast&lt;/h3&gt;
&lt;div class=&quot;level3&quot;&gt;

&lt;p&gt;
&lt;code&gt;dynamic_cast&lt;/code&gt; is exclusively used for handling polymorphism. You can cast a pointer or reference to any polymorphic type to any other class type (a polymorphic type has at least one virtual function, declared or inherited). You can use it for more than just casting downwards – you can cast sideways or even up another chain. The &lt;code&gt;dynamic_cast&lt;/code&gt; will seek out the desired object and return it if possible. If it can&amp;#039;t, it will return &lt;code&gt;nullptr&lt;/code&gt; in the case of a pointer, or throw &lt;code&gt;std::bad_cast&lt;/code&gt; in the case of a reference.
&lt;/p&gt;

&lt;p&gt;
&lt;code&gt;dynamic_cast&lt;/code&gt; has some limitations, though. It doesn&amp;#039;t work if there are multiple objects of the same type in the inheritance hierarchy (the so-called &amp;#039;dreaded diamond&amp;#039;) and you aren&amp;#039;t using &lt;code&gt;virtual&lt;/code&gt; inheritance. It also can only go through public inheritance - it will always fail to travel through &lt;code&gt;protected&lt;/code&gt; or &lt;code&gt;private&lt;/code&gt; inheritance. This is rarely an issue, however, as such forms of inheritance are rare.
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT{&amp;quot;target&amp;quot;:&amp;quot;section&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;dynamic_cast&amp;quot;,&amp;quot;hid&amp;quot;:&amp;quot;dynamic_cast&amp;quot;,&amp;quot;codeblockOffset&amp;quot;:0,&amp;quot;secid&amp;quot;:5,&amp;quot;range&amp;quot;:&amp;quot;2084-3064&amp;quot;} --&gt;
&lt;h3 class=&quot;sectionedit6&quot; id=&quot;reinterpret_cast&quot;&gt;reinterpret_cast&lt;/h3&gt;
&lt;div class=&quot;level3&quot;&gt;

&lt;p&gt;
&lt;code&gt;reinterpret_cast&lt;/code&gt; is the most dangerous cast, and should be used very sparingly. It turns one type directly into another — such as casting the value from one pointer to another, or storing a pointer in an &lt;code&gt;int&lt;/code&gt;, or all sorts of other nasty things. Largely, the only guarantee you get with &lt;code&gt;reinterpret_cast&lt;/code&gt; is that normally if you cast the result back to the original type, you will get the exact same value (but &lt;strong&gt;*not&lt;/strong&gt;* if the intermediate type is smaller than the original type). There are a number of conversions that &lt;strong&gt;&lt;code&gt;reinterpret_cast&lt;/code&gt;&lt;/strong&gt; cannot do, too. It&amp;#039;s often abused for particularly weird conversions and bit manipulations, like turning a raw data stream into actual data, or storing data in the low bits of a pointer to aligned data. For those cases, see &lt;code&gt;std::bit_cast&lt;/code&gt;.
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT{&amp;quot;target&amp;quot;:&amp;quot;section&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;reinterpret_cast&amp;quot;,&amp;quot;hid&amp;quot;:&amp;quot;reinterpret_cast&amp;quot;,&amp;quot;codeblockOffset&amp;quot;:0,&amp;quot;secid&amp;quot;:6,&amp;quot;range&amp;quot;:&amp;quot;3065-3894&amp;quot;} --&gt;
&lt;h3 class=&quot;sectionedit7&quot; id=&quot;c-style_cast_and_function-style_cast&quot;&gt;C-style cast and function-style cast&lt;/h3&gt;
&lt;div class=&quot;level3&quot;&gt;

&lt;p&gt;
C-style cast and function-style cast are casts using &lt;code&gt;(type)object&lt;/code&gt; or &lt;code&gt;type(object)&lt;/code&gt;, respectively, and are functionally equivalent. They are defined as the first of the following which succeeds:
&lt;/p&gt;
&lt;ol&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt; &lt;code&gt;const_cast&lt;/code&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt; &lt;code&gt;static_cast&lt;/code&gt; (though ignoring access restrictions)&lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt; &lt;code&gt;static_cast&lt;/code&gt; (see above), then &lt;code&gt;const_cast&lt;/code&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt; &lt;code&gt;reinterpret_cast&lt;/code&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt; &lt;code&gt;reinterpret_cast&lt;/code&gt;, then &lt;code&gt;const_cast&lt;/code&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;
It can therefore be used as a replacement for other casts in some instances, but can be extremely dangerous because of the ability to devolve into a &lt;code&gt;reinterpret_cast&lt;/code&gt;, and the latter should be preferred when explicit casting is needed, unless you are sure &lt;code&gt;static_cast&lt;/code&gt; will succeed or &lt;code&gt;reinterpret_cast&lt;/code&gt; will fail. Even then, consider the longer, more explicit option.
&lt;/p&gt;

&lt;p&gt;
C-style casts also ignore access control when performing a &lt;code&gt;static_cast&lt;/code&gt;, which means that they have the ability to perform an operation that no other cast can. This is mostly a kludge, though, and in my mind is just another reason to avoid C-style casts.
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT{&amp;quot;target&amp;quot;:&amp;quot;section&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;C-style cast and function-style cast&amp;quot;,&amp;quot;hid&amp;quot;:&amp;quot;c-style_cast_and_function-style_cast&amp;quot;,&amp;quot;codeblockOffset&amp;quot;:0,&amp;quot;secid&amp;quot;:7,&amp;quot;range&amp;quot;:&amp;quot;3895-4984&amp;quot;} --&gt;
&lt;h3 class=&quot;sectionedit8&quot; id=&quot;stdbit_cast_c_20&quot;&gt;std::bit_cast [C++20]&lt;/h3&gt;
&lt;div class=&quot;level3&quot;&gt;

&lt;p&gt;
&lt;code&gt;std::bit_cast&lt;/code&gt; copies the bits and bytes of the source object (its representation) directly into a new object of the target type.  It&amp;#039;s a standards-compliant way to do type punning.  If you find yourself writing &lt;code&gt;*reinterpret_cast&amp;lt;SomeType*&amp;gt;(&amp;amp;x)&lt;/code&gt;, you probably should use &lt;code&gt;std::bit_cast&amp;lt;SomeType&amp;gt;(x)&lt;/code&gt; instead.
&lt;/p&gt;

&lt;p&gt;
&lt;code&gt;std::bit_cast&lt;/code&gt; is declared in &lt;code&gt;&amp;lt;bit&amp;gt;&lt;/code&gt;.  The objects must be the same size and be trivially copyable.  If you can&amp;#039;t yet use C++20, use &lt;code&gt;memcpy&lt;/code&gt; to copy the source value into a variable of the desired type.
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT{&amp;quot;target&amp;quot;:&amp;quot;section&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;std::bit_cast [C++20]&amp;quot;,&amp;quot;hid&amp;quot;:&amp;quot;stdbit_cast_c_20&amp;quot;,&amp;quot;codeblockOffset&amp;quot;:0,&amp;quot;secid&amp;quot;:8,&amp;quot;range&amp;quot;:&amp;quot;4985-&amp;quot;} --&gt;</description>
    </item>
    <item rdf:about="http://wiki.nathancampos.me/doku.php?id=lang:python">
        <dc:format>text/html</dc:format>
        <dc:date>2023-05-08T07:27:58+00:00</dc:date>
        <dc:creator>Anonymous (anonymous@undisclosed.example.com)</dc:creator>
        <title>python</title>
        <link>http://wiki.nathancampos.me/doku.php?id=lang:python</link>
        <description>
&lt;h1 class=&quot;sectionedit1&quot; id=&quot;python&quot;&gt;Python&lt;/h1&gt;
&lt;div class=&quot;level1&quot;&gt;

&lt;p&gt;
The language of the snake! Known to be, according to the &lt;a href=&quot;https://www.tiobe.com/tiobe-index/&quot; class=&quot;urlextern&quot; title=&quot;https://www.tiobe.com/tiobe-index/&quot; rel=&quot;ugc nofollow&quot;&gt;TIOBE Index&lt;/a&gt;, the most popular programming language &lt;sup&gt;at least in 2023&lt;/sup&gt;, used by countless beginner and senior developers throughout the world to create hello world applications and super complex, highly accurate, mind-blowing machine learning and artificial intelligence models, all of this with its horrible whitespace-based syntax and awful looking docblocks.
&lt;/p&gt;

&lt;p&gt;
Even though it has a lot going for it and against it, it is the most popular language, and knowing/using it gives us a great advantage in terms of collaboration and code reuse. Given its size, it&amp;#039;s to be expected that they might already have a library for that application that you need to build, or if you thought of something novel and want to build a library for it, most likely if you publish it you are guaranteed get many downloads and maybe even some contributions.
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT{&amp;quot;target&amp;quot;:&amp;quot;section&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;Python&amp;quot;,&amp;quot;hid&amp;quot;:&amp;quot;python&amp;quot;,&amp;quot;codeblockOffset&amp;quot;:0,&amp;quot;secid&amp;quot;:1,&amp;quot;range&amp;quot;:&amp;quot;1-963&amp;quot;} --&gt;
&lt;h2 class=&quot;sectionedit2&quot; id=&quot;project_structure&quot;&gt;Project Structure&lt;/h2&gt;
&lt;div class=&quot;level2&quot;&gt;

&lt;p&gt;
One of the most important aspects of any language is its project structure. If you are building simple one-of scripts hacked together quickly just to perform a task none of this is needed, but as soon as you want to build something that might get used by others or that you might want to publish later in a package manager you need to use the language&amp;#039;s proper structure for that. Here are some resources on the topic:
&lt;/p&gt;
&lt;ul&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt; &lt;a href=&quot;https://docs.python-guide.org/writing/structure/&quot; class=&quot;urlextern&quot; title=&quot;https://docs.python-guide.org/writing/structure/&quot; rel=&quot;ugc nofollow&quot;&gt;Structuring Your Project&lt;/a&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt; &lt;a href=&quot;https://medium.com/mlearning-ai/a-practical-guide-to-python-project-structure-and-packaging-90c7f7a04f95&quot; class=&quot;urlextern&quot; title=&quot;https://medium.com/mlearning-ai/a-practical-guide-to-python-project-structure-and-packaging-90c7f7a04f95&quot; rel=&quot;ugc nofollow&quot;&gt;Guide to Python Project Structure and Packaging&lt;/a&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt; &lt;a href=&quot;https://realpython.com/python-application-layouts/&quot; class=&quot;urlextern&quot; title=&quot;https://realpython.com/python-application-layouts/&quot; rel=&quot;ugc nofollow&quot;&gt;Python Application Layouts: A Reference&lt;/a&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;/div&gt;
&lt;!-- EDIT{&amp;quot;target&amp;quot;:&amp;quot;section&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;Project Structure&amp;quot;,&amp;quot;hid&amp;quot;:&amp;quot;project_structure&amp;quot;,&amp;quot;codeblockOffset&amp;quot;:0,&amp;quot;secid&amp;quot;:2,&amp;quot;range&amp;quot;:&amp;quot;964-&amp;quot;} --&gt;</description>
    </item>
</rdf:RDF>
