Boost C++ Libraries: Ticket #11602: boost.variant constructor accepts any type as parameter https://svn.boost.org/trac10/ticket/11602 <p> There is one "converting constructor" in boost.variant which accepts any type - even those which are not convertible to instantiation types of boost.variant. This causes that writing function accepting some specific variant instantiation cause that this function is on candidate list of any type in your program. </p> <p> The problem is with this constructor: </p> <pre class="wiki"> template &lt;typename T&gt; variant(const T&amp; operand) { convert_construct(operand, 1L); } </pre><p> And the small code presenting the issue: </p> <pre class="wiki">#include &lt;iostream&gt; #include &lt;boost/variant.hpp&gt; // types not related in any way class A {}; class B {}; class C {}; class D {}; using ABC_variant = boost::variant&lt;A,B,C&gt;; std::ostream&amp; operator &lt;&lt; (std::ostream&amp; os, const ABC_variant&amp;) { return os &lt;&lt; "ABC"; } int main() { D d; std::cout &lt;&lt; d; } </pre><p> As described in <a class="ext-link" href="http://stackoverflow.com/questions/32275725/boostvariant-construction-weirdness-its-ctor-accepts-everything"><span class="icon">​</span>http://stackoverflow.com/questions/32275725/boostvariant-construction-weirdness-its-ctor-accepts-everything</a> </p> <p> compiler (gcc4.9) tries to use ostream operator with this diagnostic: </p> <p> /usr/include/boost/variant/variant.hpp:1591:38: error: no matching function for call to 'boost::variant&lt;A, B, C&gt;::initializer::initialize(void*, D&amp;)' </p> <blockquote> <p> initializer::initialize( </p> </blockquote> <p> However it should simple report that ostream operator for D is missing. </p> <p> One simple way is to declare this boost.variant constructor as explicit (what probably would impact to many client code) or to make some restriction in this constructor like this C++11 way: </p> <pre class="wiki">template &lt;typename T, typename ...C&gt; struct IsAnyOf; template &lt;typename T, typename ...C&gt; struct IsAnyOf&lt;T,T,C...&gt; : std::true_type {}; template &lt;typename T&gt; struct IsAnyOf&lt;T&gt; : std::false_type {}; template &lt;typename T, typename C1, typename ...C&gt; struct IsAnyOf&lt;T,C1,C...&gt; : IsAnyOf&lt;T, C...&gt; {}; template &lt;typename T, typename EnableIf = typename std::enable_if&lt;IsAnyOf&lt;VariantType...&gt;::value&gt;::type&gt; variant(const T&amp; operand) </pre><p> BR, Piotr Nycz (piotrwn1 @ gmail com) </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/11602 Trac 1.4.3 piotrwn1@… Tue, 01 Sep 2015 08:31:17 GMT <link>https://svn.boost.org/trac10/ticket/11602#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11602#comment:1</guid> <description> <p> Maybe better would to use <a class="missing wiki">IsConvertibleToAnyOf</a> instead of <a class="missing wiki">IsAnyOf</a>: </p> <pre class="wiki">template &lt;typename T, typename ...C&gt; struct IsConvertibleToAnyOf; template &lt;typename T&gt; struct IsConvertibleToAnyOf&lt;T&gt; : std::false_type {}; template &lt;typename T, typename C1, typename ...C&gt; struct IsConvertibleToAnyOf&lt;T,C1,C...&gt; : std::integral_constant&lt;bool, std::is_convertible&lt;T,C1&gt;::value or IsConvertibleToAnyOf&lt;T,C...&gt;::value&gt; {}; </pre><p> BR, </p> <p> Piotr Nycz </p> </description> <category>Ticket</category> </item> <item> <author>raad@…</author> <pubDate>Thu, 14 Apr 2016 21:15:15 GMT</pubDate> <title>cc set https://svn.boost.org/trac10/ticket/11602#comment:2 https://svn.boost.org/trac10/ticket/11602#comment:2 <ul> <li><strong>cc</strong> <span class="trac-author">raad@…</span> added </li> </ul> Ticket raad@… Thu, 14 Apr 2016 21:39:01 GMT <link>https://svn.boost.org/trac10/ticket/11602#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11602#comment:3</guid> <description> <p> This problem also makes boost::variant unusable in C++17's std::tuple as implemented in MSVC 14 Update 2, affecting boost::signals2 (ticket <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/12123" title="#12123: Bugs: Using Boost.Signals2 with Boost.Variant leads to compilation error, ... (closed: fixed)">#12123</a>). </p> <p> Ticket <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/5871" title="#5871: Bugs: variant constructible/assignable from anything (closed: fixed)">#5871</a> describes the same problem. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Fri, 27 May 2016 22:39:06 GMT</pubDate> <title>owner, status, milestone changed https://svn.boost.org/trac10/ticket/11602#comment:4 https://svn.boost.org/trac10/ticket/11602#comment:4 <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">new</span> → <span class="trac-field-new">assigned</span> </li> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.62.0</span> </li> </ul> Ticket Antony Polukhin Fri, 27 May 2016 22:39:24 GMT <link>https://svn.boost.org/trac10/ticket/11602#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11602#comment:5</guid> <description> <p> Fixed in <a class="ext-link" href="https://github.com/boostorg/variant/commit/b3650685f941a0c35cadfd878a185f274e132788"><span class="icon">​</span>b3650685 develop</a>, will be merged to the master branch as soon as the regression tests will cycle. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Sat, 29 Oct 2016 18:09:46 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/11602#comment:6 https://svn.boost.org/trac10/ticket/11602#comment:6 <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-new">fixed</span> </li> </ul> Ticket