Boost C++ Libraries: Ticket #11276: Ability to hold already type erased object into a type erased reference. https://svn.boost.org/trac10/ticket/11276 <p> I would like to ask for an enhancement to this fantastic library, that is making me so happy in the absence of non-library solutions in c++ :) </p> <p> This problem came up in my code: </p> <pre class="wiki"> class my_concept : public any&lt;requirements&gt; { using base = any&lt;requirements&gt;; public: using base::base; }; class my_concept_ref : public any&lt;requirements, _self &amp;&gt; { using base = any&lt;requirements, _self&amp;&gt;; public: template &lt;class... Args&gt; my_concept_ref(Args &amp;&amp;... args) : base(std::forward&lt;Args&gt;(args)...) {} }; //client code vector&lt;my_concept&gt; vec; // vec.push_back(my_concrete_model{}); my_concept_ref get_a_type_erased_type_in_a_ref(vector&lt;my_concept&gt; &amp; vec) { return vec[0]; } //Throws bad_cast: auto &amp; want_my_model = any_cast&lt;my_concrete_model&amp;&gt;(get_a_type_erased_type_in_a_ref()); //I must do this: auto &amp; will_give_me_my_model = any_cast&lt;my_concrete_model&amp;&gt;(any_cast&lt;my_concept &amp;&gt;( get_a_type_erased_type_in_a_ref()); </pre><p> The problem here is that when the constructor for my_concept_ref takes an already type erased type from the vector (my_concept), it wraps that concrete type, which is a model of my_concept, but it was already type_erased, and not the internal concrete model (my_concrete_model). </p> <p> What could be implemented is automatic unwrapping of a type erased type so that I can do this: </p> <pre class="wiki"> //Throws bad_cast: auto &amp; want_my_model = any_cast&lt;my_concrete_model&amp;&gt;(get_a_type_erased_type_in_a_ref()); </pre><p> I think this should work. There is no easy workaround but to cast twice. I think the runtime concept constructor should check wether the class being taking in the constructor is actually already a type erased one based on detecting any&lt;requirements&gt;. I do not want to use in my code both my_concept &amp; and my_concept_ref depending on wether I wrapped my concrete class or not in a type erased wrapper. This would give the library more uniformity and makes sense conceptually, because my_concept &amp; can only be used in case I wrapped my type in a my_concept &amp;, which is not always the case. </p> <p> Great work, by the way! I am trying to make heavy use of the library and so far it is serving me very well. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/11276 Trac 1.4.3 Steven Watanabe Fri, 08 May 2015 16:35:48 GMT <link>https://svn.boost.org/trac10/ticket/11276#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11276#comment:1</guid> <description> <p> Is there a good reason that you need to inherit from any instead of just using a typedef? Your example will work with any&lt;C, _self&gt; and any&lt;C, _self&amp;&gt;. If you really need to create your own wrapper, then you need to define the extra constructor overloads needed to make it work. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Sat, 09 May 2015 00:29:28 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/11276#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11276#comment:2</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/11276#comment:1" title="Comment 1">steven_watanabe</a>: </p> <blockquote class="citation"> <p> Is there a good reason that you need to inherit from any instead of just using a typedef? Your example will work with any&lt;C, _self&gt; and any&lt;C, _self&amp;&gt;. If you really need to create your own wrapper, then you need to define the extra constructor overloads needed to make it work. </p> </blockquote> <p> The ability to forward declare my types is important. With typedef and using this is not possible. </p> </description> <category>Ticket</category> </item> </channel> </rss>