Opened 10 years ago

#7261 new Bugs

Boost.Units quantity output overflows ostream width field

Reported by: Paul A. Bristow Owned by: Steven Watanabe
Milestone: To Be Determined Component: units
Version: Boost 1.52.0 Severity: Problem
Keywords: units ostream width overflow Cc: boost@…

Description

Boost.Units quantity output overflows ostream width field

quantity<length> ql = 2345.6 * meters;

std::cout << std::setw(20) << ql << std::endl;

outputs 22 chars instead of 20 chars.

The reason is that the first item value() is output using the ios width setting, but the second unit() 'm' (and separating space) is not.

if os.width() > 0 then a way is to build a single string of the quantity with the width specified for example using an ostringstream (otherwise, outputing value and unit (perhaps autoprefixed to km) severally would overflow the width).

(else if os.width() <= 0 then output directly to ostream os as at present).

A test and a patch to deal with this is attached.

(This is not necessarily the most efficient way to deal with this but appears to work).

Attachments (3)

io.hpp.diff (1.8 KB ) - added by Paul A. Bristow 10 years ago.
Unified diff for units/boost/io.hpp
units_measure_width_test.cpp (3.3 KB ) - added by Paul A. Bristow 10 years ago.
Test file for changes to IO.hpp to correct width overflow
measurement.hpp.diff (9.7 KB ) - added by Paul A. Bristow 10 years ago.
Diff for changes to /example/measurement.hpp to honor width

Download all attachments as: .zip

Change History (3)

by Paul A. Bristow, 10 years ago

Attachment: io.hpp.diff added

Unified diff for units/boost/io.hpp

by Paul A. Bristow, 10 years ago

Test file for changes to IO.hpp to correct width overflow

by Paul A. Bristow, 10 years ago

Attachment: measurement.hpp.diff added

Diff for changes to /example/measurement.hpp to honor width

Note: See TracTickets for help on using tickets.