Boost C++ Libraries: Ticket #4658: (boost::format("%u") % (unsigned char)2).str(); error result https://svn.boost.org/trac10/ticket/4658 <p> (boost::format("%u") % (unsigned char)2).str(); (boost::format("%d") % (char)2).str(); error result </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/4658 Trac 1.4.3 James E. King, III Thu, 12 Oct 2017 02:14:22 GMT owner, status changed https://svn.boost.org/trac10/ticket/4658#comment:1 https://svn.boost.org/trac10/ticket/4658#comment:1 <ul> <li><strong>owner</strong> changed from <span class="trac-author">Samuel Krempp</span> to <span class="trac-author">James E. King, III</span> </li> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> <p> Is this a fair test to represent the report? There wasn't much to go on: </p> <pre class="wiki"> // trac-4658 s = (format("%1%") % (unsigned char)2).str(); BOOST_CHECK_EQUAL(1, s.length()); BOOST_CHECK_EQUAL(0x02, s.at(0)); s = (format("%u") % (unsigned char)2).str(); BOOST_CHECK_EQUAL(1, s.length()); BOOST_CHECK_EQUAL('2', s.at(0)); s = (format("%1%") % (char)3).str(); BOOST_CHECK_EQUAL(1, s.length()); BOOST_CHECK_EQUAL(0x03, s.at(0)); s = (format("%d") % (char)3).str(); BOOST_CHECK_EQUAL(1, s.length()); BOOST_CHECK_EQUAL('3', s.at(0)); </pre> Ticket James E. King, III Thu, 12 Oct 2017 17:58:58 GMT <link>https://svn.boost.org/trac10/ticket/4658#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4658#comment:2</guid> <description> <p> <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/9360" title="#9360: Bugs: boost::format char-&gt;hex (closed: duplicate)">#9360</a> also details the following, which I verified: </p> <pre class="wiki"> // https://svn.boost.org/trac10/ticket/9360 unsigned char c = 0x55; s = (boost::format("0x%02X") % c).str(); BOOST_CHECK_EQUAL(std::string("0x55"), s); Running 1 test case... format_test3.cpp(134): error: in "test_main_caller(_argc,_argv_)": check std::string("0x55") == s has failed [0x55 != 0x0U] </pre><p> If you change the type of c to an unsigned short, or unsigned int, then the check passes, so it is specific to char handling, just as the original report here. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>James E. King, III</dc:creator> <pubDate>Wed, 18 Oct 2017 13:12:54 GMT</pubDate> <title>status changed; resolution set; milestone deleted https://svn.boost.org/trac10/ticket/4658#comment:3 https://svn.boost.org/trac10/ticket/4658#comment:3 <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">wontfix</span> </li> <li><strong>milestone</strong> <span class="trac-field-deleted">Boost 1.44.0</span> </li> </ul> <p> Resolving this as wontfix because it is working as designed. The argument type modifiers are ignored completely by Boost.Format for type safety reasons as defined in the documentation for the library. </p> <p> A workaround is to cast the value to an integer before adding it with operator %. </p> <p> I'm thinking about changing this behavior in future releases, and allowing full ISO C99 compatibility, possibly by simply leveraging snprintf when a C style format specification with a conversion specifier is used. Anything that happens in this area would be fully backwards compatible through preprocessor macros. We'll see. </p> Ticket