Opened 12 years ago

Closed 5 years ago

#4658 closed Bugs (wontfix)

(boost::format("%u") % (unsigned char)2).str(); error result

Reported by: anonymous Owned by: James E. King, III
Milestone: Component: format
Version: Boost 1.44.0 Severity: Problem
Keywords: Cc:

Description

(boost::format("%u") % (unsigned char)2).str(); (boost::format("%d") % (char)2).str(); error result

Change History (3)

comment:1 by James E. King, III, 5 years ago

Owner: changed from Samuel Krempp to James E. King, III
Status: newassigned

Is this a fair test to represent the report? There wasn't much to go on:

    // 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));

comment:2 by James E. King, III, 5 years ago

#9360 also details the following, which I verified:

    // 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]

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.

comment:3 by James E. King, III, 5 years ago

Milestone: Boost 1.44.0
Resolution: wontfix
Status: assignedclosed

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.

A workaround is to cast the value to an integer before adding it with operator %.

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.

Note: See TracTickets for help on using tickets.