Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I don't think you can write the actual type in a fully type-safe way in C++, because all of the types used when instantiating a template must be complete when you instantiate the template, and what you basically need to do is something like:</p> <pre><code>typedef boost::variant&lt;Value, std::map&lt;Key, ExtendedValue&gt; &gt; ExtendedValue; typedef std::map&lt;Key, ExtendedValue&gt; MyMap; </code></pre> <p>This causes a recursion in the template dependencies: in order to instantiate <code>std::map</code>, <code>ExtendedValue</code> must be complete, but in order to instantiate <code>ExtendedValue</code>, <code>std::map</code> must be complete.</p> <p>You should be able to create a less type-safe version using: </p> <pre><code>typedef std::map&lt;Key, boost::any&gt; MyMap; </code></pre> <p>As long as all you put into the map have type <code>Value</code> or <code>MyMap</code>, this should work. (I've done something similar in Java, where the mapped type was <code>Object</code>.)</p> <p>Alternatively, you could map to a boost::variant which contained a pointer to the map, rather than the map itself. Even this is difficult to name unless you you wrap the map in a class, however. You can easily forward declare a class, and use pointers to it, but you can't declare an instantiation of a template until the types used in the template are at least known. So you'ld have to write something like:</p> <pre><code>class MyMap; typedef boost::variant&lt;Value, MyMap*&gt; ExtendedValue; class MyMap : private std::map&lt;Key, ExtendedValue&gt; { public: using ...; }; </code></pre> <p>Since you have to use pointers, this might be the way to go anyway; you could then wrap the member functions you need to ensure correct member management as well. (In which case, a function like <code>operator[]</code> would probably have to return a proxy, so that you could intercept writes, and do the allocation.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload