{"id":3982,"date":"2014-03-30T06:56:58","date_gmt":"2014-03-30T06:56:58","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/boost-how-to-create-a-map-for-types-selection-collection-of-common-programming-errors\/"},"modified":"2014-03-30T06:56:58","modified_gmt":"2014-03-30T06:56:58","slug":"boost-how-to-create-a-map-for-types-selection-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/boost-how-to-create-a-map-for-types-selection-collection-of-common-programming-errors\/","title":{"rendered":"Boost how to create a map for types selection?-Collection of common programming errors"},"content":{"rendered":"<p>For that, you would need a heterogeneous map &#8211; that is, its elements can be of different types. Furthermore you would need the ability to return <strong>types<\/strong> from functions, not just variables.<\/p>\n<p>Now, a heterogeneous map would be possible with <code>Boost.Variant<\/code> or a simple <code>union<\/code>, but that binds it to compile time: we need to know every type that is possible to create that variant\/union.<br \/>\nOf course a <code>Boost.Any<\/code> would be possible to store everything and its dog, but the problem strikes again: you need to extract the <em>real<\/em> type out of that <code>Boost.Any<\/code> again. The problem repeats itself. And if you know the real type, you can aswell just make a variant\/union and save yourself the <code>any_cast<\/code> trouble.<\/p>\n<p>Now, for another troublesome thing:<\/p>\n<pre><code>m.get\n<\/code><\/pre>\n<p>To make the above line work, you&#8217;d need two features that C++ doesn&#8217;t have: the ability to <strong>return types<\/strong> and <strong>runtime templates<\/strong>. Lets ignore the first point for a moment.<br \/>\nTemplates are <strong>compile-time<\/strong>, and the <code>get<\/code> function is such a template. Now, to use that template, your <code>myMap<\/code> would need to be able to return types at <strong>compile-time<\/strong>, while getting populated at <strong>runtime<\/strong>. See the contradiction? That&#8217;s why runtime templates would be needed.<\/p>\n<p>Sadly, exactly those three things are not possible (or extremely hard and very very limited) in C++ at runtime: <strong>heterogeneous<\/strong> data types (without constant size), returning <strong>types<\/strong> and <strong>templates<\/strong>.<br \/>\nEverything that involves types needs to be done at <strong>compile-time<\/strong>. This blogpost by @Gman somewhat correlates with that problem. It&#8217;s definitly worth a read if you want to know what C++ just <em>can&#8217;t<\/em> do.<\/p>\n<p>So, to conclude: You&#8217;ll need to rethink and refactor your problem and solution. \ud83d\ude10<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For that, you would need a heterogeneous map &#8211; that is, its elements can be of different types. Furthermore you would need the ability to return types from functions, not just variables. Now, a heterogeneous map would be possible with Boost.Variant or a simple union, but that binds it to compile time: we need to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3982","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3982","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3982"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3982\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}