Boost C++ Libraries: Ticket #547: [variant] Compile time checked getter https://svn.boost.org/trac10/ticket/547 <pre class="wiki">[Sorry if anyone feels bad about this, I already posted it in usera and devel lists, with no answer at all] During a redesign, I began using boost::variant, and I detected that trying to get a value of a type not included in variant type-list resulted in run-time error (cast or null pointer). I feel that a variant should only try to return a valid type from its type-list, otherwise giving a compiler error, and reserve runttime errors for that case when someone tries to get a value from a type contained in the typelist, but not the actual type contained in the actual object. In such a way, I have made a little redesign on boost::get and called it boost::checked_get: namespace boost { template &lt;typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) &gt; inline typename add_pointer&lt;U&gt;::type checked_get( boost::variant&lt; BOOST_VARIANT_ENUM_PARAMS(T) &gt;* operand BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U) ) { typedef boost::variant&lt; BOOST_VARIANT_ENUM_PARAMS(T) &gt; Variant; /* throw a compile-time error if type is not contained on type list */ BOOST_STATIC_ASSERT((!is_same&lt; typename mpl::find&lt;typename Variant::types,U&gt;::type, typename mpl::end&lt;typename Variant::types&gt;::type&gt;::value)); typedef typename add_pointer&lt;U&gt;::type U_ptr; if (!operand) return static_cast&lt;U_ptr&gt;(0); detail::variant::get_visitor&lt;U&gt; v; return operand-&gt;apply_visitor(v); } } // end of addition to namespace boost And, of course, the rest of the templates needed for const *, ref and const&amp;.. I think it would be interesting to include it in boost libraries. In fact, I use it intensely. Best regards, Zara (yozara@terra.es) </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/547 Trac 1.4.3 yozara Wed, 11 Jan 2006 07:03:03 GMT <link>https://svn.boost.org/trac10/ticket/547#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/547#comment:1</guid> <description> <pre class="wiki">Logged In: YES user_id=1424465 (It is me again, but this time I am logged in) Best regards, Zara </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>ebf</dc:creator> <pubDate>Thu, 12 Jan 2006 01:12:00 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/547#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/547#comment:2</guid> <description> <pre class="wiki">Logged In: YES user_id=120427 Hi Zara, I like this idea, and in fact a similar feature was proposed by another user as well some time ago. I don't have time right now though to add the tests and documentation that would need to accompany this change... Another issue to think about: I would imagine checking is likely almost always useful when using a variant in non-generic contexts, but I can envision a generic context where unchecked behavior would be desirable. I am not sure then whether it is better to have the default "get" be unchecked (as currently) or checked (as proposed). Leaving get as unchecked does not introduce backward compatibility issues, but it may be the right thing to do (and then introduced an "unchecked_get" or something similar). I'd be interested in your thoughts. Eric </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>yozara</dc:creator> <pubDate>Tue, 24 Jan 2006 09:31:33 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/547#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/547#comment:3</guid> <description> <pre class="wiki">Logged In: YES user_id=1424465 Well, my proposal is to keep get as is, and add the checked_get family of functions. No code will be broken, and it would be easy to upgrade it with new funcitons, if desired. Zara </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>ebf</dc:creator> <pubDate>Thu, 24 May 2007 18:54:28 GMT</pubDate> <title>owner, status, component changed; severity set https://svn.boost.org/trac10/ticket/547#comment:4 https://svn.boost.org/trac10/ticket/547#comment:4 <ul> <li><strong>owner</strong> changed from <span class="trac-author">nobody</span> to <span class="trac-author">ebf</span> </li> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">new</span> </li> <li><strong>component</strong> <span class="trac-field-old">None</span> → <span class="trac-field-new">variant</span> </li> <li><strong>severity</strong> → <span class="trac-field-new">Showstopper</span> </li> </ul> Ticket ebf Thu, 24 May 2007 18:54:45 GMT severity changed https://svn.boost.org/trac10/ticket/547#comment:5 https://svn.boost.org/trac10/ticket/547#comment:5 <ul> <li><strong>severity</strong> <span class="trac-field-old">Showstopper</span> → <span class="trac-field-new">Optimization</span> </li> </ul> Ticket ebf Thu, 24 May 2007 18:54:51 GMT status changed https://svn.boost.org/trac10/ticket/547#comment:6 https://svn.boost.org/trac10/ticket/547#comment:6 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> Ticket Antony Polukhin Wed, 10 Dec 2014 13:44:00 GMT owner, status, description changed; milestone set https://svn.boost.org/trac10/ticket/547#comment:7 https://svn.boost.org/trac10/ticket/547#comment:7 <ul> <li><strong>owner</strong> changed from <span class="trac-author">ebf</span> to <span class="trac-author">Antony Polukhin</span> </li> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">new</span> </li> <li><strong>description</strong> modified (<a href="/trac10/ticket/547?action=diff&amp;version=7">diff</a>) </li> <li><strong>milestone</strong> → <span class="trac-field-new">Boost 1.58.0</span> </li> </ul> <p> Series of commits were merged to develop branch to fix this issue. At this moment last commit is <a class="ext-link" href="https://github.com/boostorg/variant/commit/cbf4bbf1b1df5c3bbc65fec1f2de3351c141cfd3"><span class="icon">​</span>cbf4bbf</a> </p> Ticket Antony Polukhin Wed, 10 Dec 2014 13:44:30 GMT status changed https://svn.boost.org/trac10/ticket/547#comment:8 https://svn.boost.org/trac10/ticket/547#comment:8 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> Ticket Antony Polukhin Mon, 09 Mar 2015 11:49:11 GMT status, resolution changed https://svn.boost.org/trac10/ticket/547#comment:9 https://svn.boost.org/trac10/ticket/547#comment:9 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> <span class="trac-field-old">None</span> → <span class="trac-field-new">fixed</span> </li> </ul> Ticket