Boost C++ Libraries: Ticket #5637: use of mpl::print imposes default constructible requirement on argument https://svn.boost.org/trac10/ticket/5637 <p> I'm using the following to debug some TMP code that looks like the following. </p> <pre class="wiki">#include &lt;boost/mpl/print.hpp&gt; struct X { X(int); }; boost::mpl::print&lt; X &gt;::type x; boost::mpl::print&lt; X &gt;::type y; </pre><p> I get an error message that the type X is not default constructible. A little investigation yields the definition of mpl::print (simplified for exposition) </p> <pre class="wiki">template &lt;class T&gt; struct print : identity&lt;T&gt; { enum { n = sizeof(T) + -1 }; }; </pre><p> So invoking the print creates an instance with the default constructor which provokes the error in this case. For my purposes, I made the following change which seems to address the problem: </p> <pre class="wiki">template &lt;class T&gt; struct print : identity&lt;T *&gt; { enum { n = sizeof(T) + -1 }; }; </pre><p> I'm using MSVC 9.0. </p> <p> A couple of misceleanous issues besides this: </p> <p> On my current version of GCC - 4.3.4 I get no warning at all. </p> <p> If the same type is printed more than once, I only get the warning on the first instance. </p> <p> Robert Ramey </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5637 Trac 1.4.3 Robert Ramey Fri, 24 Jun 2011 01:15:46 GMT <link>https://svn.boost.org/trac10/ticket/5637#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5637#comment:1</guid> <description> <p> Managed to make it work for my GCC system as well ! </p> <pre class="wiki">template &lt;class T&gt; struct print : mpl::identity&lt;T *&gt; #if defined(__MWERKS__) , aux::print_base #endif { #if defined(BOOST_MSVC) enum { n = sizeof(T) + -1 }; #elif defined(__MWERKS__) void f(int); #else enum { n = # if defined(__EDG_VERSION__) aux::dependent_unsigned&lt;T&gt;::value &gt; -1 # else sizeof(T) / 0//sizeof(T) &gt; -1 # endif }; #endif }; </pre><p> Robert Ramey </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Robert Ramey</dc:creator> <pubDate>Sat, 25 Jun 2011 16:59:31 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5637#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5637#comment:2</guid> <description> <p> Damn - my patch mpl::identity&lt;T&gt; &lt;- mpl::identity&lt;T *&gt; - broke type printing of reference types. So I withdraw that suggestion. The other change:sizeof(T) &gt; -1 &lt;- sizeof(T) / 0 still stands however - at least from my version of GCC. </p> <p> Robert Ramey </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Dave Abrahams</dc:creator> <pubDate>Wed, 13 Jul 2011 18:55:48 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/5637#comment:3 https://svn.boost.org/trac10/ticket/5637#comment:3 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> <p> Replying to <a class="new ticket" href="https://svn.boost.org/trac10/ticket/5637" title="#5637: Tasks: use of mpl::print imposes default constructible requirement on argument (new)">ramey</a>: </p> <blockquote class="citation"> <p> I'm using the following to debug some TMP code that looks like the following. </p> <pre class="wiki">#include &lt;boost/mpl/print.hpp&gt; struct X { X(int); }; boost::mpl::print&lt; X &gt;::type x; boost::mpl::print&lt; X &gt;::type y; </pre><p> I get an error message that the type X is not default constructible. </p> </blockquote> <p> I don't think mpl::print is supposed to guarantee you can do that. Why don't you just do </p> <pre class="wiki">boost::mpl::print&lt;X&gt; y; </pre><p> instead? </p> <blockquote class="citation"> <p> A little investigation yields the definition of mpl::print (simplified for exposition) </p> <pre class="wiki">template &lt;class T&gt; struct print : identity&lt;T&gt; { enum { n = sizeof(T) + -1 }; }; </pre><p> So invoking the print creates an instance with the default constructor </p> </blockquote> <p> No. Even if you treat <code>mpl::print</code> as a metafunction, invoking it doesn't create an instance. Constructing an instance of its result… creates an an instance (which is of course tautological, sorry). </p> <blockquote class="citation"> <p> which provokes the error in this case. For my purposes, I made the following change which seems to address the problem: </p> <pre class="wiki">template &lt;class T&gt; struct print : identity&lt;T *&gt; { enum { n = sizeof(T) + -1 }; }; </pre><p> I'm using MSVC 9.0. </p> </blockquote> <p> Unfortunately, <code>mpl::print</code> <a class="new ticket" href="https://svn.boost.org/trac10/ticket/5637#1900" title="#5637: Tasks: use of mpl::print imposes default constructible requirement on argument (new)">is undocumented</a>, but the intention is that as a metafunction, it returns <code>T</code>, not <code>T*</code>, so that change wouldn't work. </p> <blockquote class="citation"> <p> A couple of misceleanous issues besides this: </p> <p> On my current version of GCC - 4.3.4 I get no warning at all. </p> </blockquote> <p> If you can figure out how to generate an appropriate warning for that compiler, we'd love to have a patch. </p> <blockquote class="citation"> <p> If the same type is printed more than once, I only get the warning on the first instance. </p> </blockquote> <p> Nothing we can do about the latter; it's just an artifact of your compiler's behavior. </p> Ticket Dave Abrahams Wed, 13 Jul 2011 18:57:48 GMT <link>https://svn.boost.org/trac10/ticket/5637#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5637#comment:4</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/5637#comment:2" title="Comment 2">ramey</a>: </p> <blockquote class="citation"> <p> The other change:sizeof(T) &gt; -1 &lt;- sizeof(T) / 0 still stands however - at least from my version of GCC. </p> </blockquote> <p> As far as I know, division by zero is still a hard error. <code>mpl::print</code> is supposed to generate warnings. If you're satisfied with an error, I'm sure you can figure out how to do that without using <code>print</code>. </p> </description> <category>Ticket</category> </item> <item> <author>pmachata@…</author> <pubDate>Thu, 18 Jul 2013 18:03:23 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5637#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5637#comment:5</guid> <description> <p> It seems that using NULL to initialize the enum value tickles GCC the right way to produce a warning if the template is actually used (unlike division by zero, which produces one warning always, and optionally more warnings if print is actually "invoked"). It's also enabled by default, unlike signed/unsigned compare that's used in the current solution. </p> <p> One downside is that we get a warning only for the first failure in a translation unit. What's considered more important: that we have one warning always, or that we get all citations, but only with -Wall? </p> </description> <category>Ticket</category> </item> <item> <author>pmachata@…</author> <pubDate>Thu, 18 Jul 2013 19:39:25 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5637#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5637#comment:6</guid> <description> <p> ... alternatively, I suppose, there could be both. </p> </description> <category>Ticket</category> </item> <item> <author>pmachata@…</author> <pubDate>Thu, 18 Jul 2013 21:45:45 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5637#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5637#comment:7</guid> <description> <p> Apologies, I was interpreting the output wrong. Initialization by NULL appears to do exactly what we need. </p> <p> From what I can figure out from GCC sources, this warning is controlled by -Wconversion-null, which has been on by default since it was introduced in 2010 (4.5.0). Before that, the warning was emitted as well, keyed to -Wconversion, which was off even in -Wall. From the time it was written in 2000 until 2006 (4.1.0), the warning was always emitted and couldn't even be turned off. So after 4.1.0 up until 4.5.0, it's more advantageous to use the signed/unsigned comparison (which is on at least in -Wall), otherwise the NULL conversion is better. </p> </description> <category>Ticket</category> </item> <item> <author>pmachata@…</author> <pubDate>Thu, 18 Jul 2013 23:29:21 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5637#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5637#comment:8</guid> <description> <p> Comparison of values belonging to different enums is even better. That appears to be available since 1996, and is bound to always-on -Wenum-compare since 2008. </p> </description> <category>Ticket</category> </item> <item> <author>pmachata@…</author> <pubDate>Fri, 19 Jul 2013 00:09:35 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/5637 https://svn.boost.org/trac10/ticket/5637 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">boost-1.54.0-mpl-print.patch</span> </li> </ul> <p> Fix mpl::print in GCC </p> Ticket Dave Abrahams Sat, 20 Jul 2013 17:08:36 GMT status changed; resolution deleted https://svn.boost.org/trac10/ticket/5637#comment:9 https://svn.boost.org/trac10/ticket/5637#comment:9 <ul> <li><strong>status</strong> <span class="trac-field-old">closed</span> → <span class="trac-field-new">reopened</span> </li> <li><strong>resolution</strong> <span class="trac-field-deleted">invalid</span> </li> </ul> Ticket Dave Abrahams Sat, 20 Jul 2013 17:09:02 GMT owner, status changed https://svn.boost.org/trac10/ticket/5637#comment:10 https://svn.boost.org/trac10/ticket/5637#comment:10 <ul> <li><strong>owner</strong> changed from <span class="trac-author">Dave Abrahams</span> to <span class="trac-author">Aleksey Gurtovoy</span> </li> <li><strong>status</strong> <span class="trac-field-old">reopened</span> → <span class="trac-field-new">new</span> </li> </ul> Ticket Jonathan Wakely <jwakely.boost@…> Tue, 15 Sep 2015 14:36:29 GMT <link>https://svn.boost.org/trac10/ticket/5637#comment:11 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5637#comment:11</guid> <description> <p> Hmm, the GCC patch no longer seems to work, or I just can't figure out what mpl::print is meant to do. </p> </description> <category>Ticket</category> </item> <item> <author>Jonathan Wakely <jwakely.boost@…></author> <pubDate>Tue, 15 Sep 2015 14:44:48 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5637#comment:12 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5637#comment:12</guid> <description> <p> It stopped working with GCC 4.8 </p> </description> <category>Ticket</category> </item> </channel> </rss>