Std::visit is everything wrong with modern C++
This blog post is not to be taken seriously in my opinion. For one thing, `printf` is obsolete in C++. This is how you should output values in a tagged union:<p><pre><code>    std::variant&lt;size_t, std::string&gt; var = &quot;test&quot;;
    std::visit([](auto &amp;&amp;val) { std::cout &lt;&lt; val; }, var);
</code></pre>
If you also want to print the name of the type of the variable, along with the value itself, well, C++ doesn&#x27;t have reflection (yet). So you&#x27;re going to have to write a function that takes a value and returns its type as a string, which you can already do using the typeid operator. This is basically implementing reflection yourself, it is cumberstone but doesn&#x27;t require advanced template programming.<p>There no need for any of the madness with explicit types inside the visitor lambda, which is deemed as neccesary by the author.<p>Edit: here&#x27;s another way to map types to strings. So no templates necessary at all for this entire problem.<p><pre><code>    std::map&lt;std::type_index, std::string&gt; typeIdxToString;
    typeIdxToString[typeid(std::string)] = &quot;string&quot;;
    typeIdxToString[typeid(int)] = &quot;int&quot;;
    &#x2F;&#x2F; ...
</code></pre>
Edit2: Turns out C++ has some form of reflection after all. You can just use:<p><pre><code>    typeid(val).name()
</code></pre>
If your compiler supports pretty names.