Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions include/boost/rational.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -965,11 +965,12 @@ std::istream& operator>> (std::istream& is, rational<IntType>& r)
}

// Add manipulators for output format?
template <typename IntType>
std::ostream& operator<< (std::ostream& os, const rational<IntType>& r)
template <typename IntType, typename Ch, typename Tr>
std::basic_ostream<Ch, Tr>& operator<< (std::basic_ostream<Ch, Tr>& os,
const rational<IntType>& r)
{
// The slash directly precedes the denominator, which has no prefixes.
std::ostringstream ss;
std::basic_ostringstream<Ch, Tr> ss;

ss.copyfmt( os );
ss.tie( NULL );
Expand All @@ -978,12 +979,12 @@ std::ostream& operator<< (std::ostream& os, const rational<IntType>& r)
ss << std::noshowpos << std::noshowbase << '/' << r.denominator();

// The numerator holds the showpos, internal, and showbase flags.
std::string const tail = ss.str();
std::basic_string<Ch, Tr> const tail = ss.str();
std::streamsize const w =
os.width() - static_cast<std::streamsize>( tail.size() );

ss.clear();
ss.str( "" );
ss.str( std::basic_string<Ch, Tr>() );
ss.flags( os.flags() );
ss << std::setw( w < 0 || (os.flags() & std::ios::adjustfield) !=
std::ios::internal ? 0 : w ) << r.numerator();
Expand Down
3 changes: 2 additions & 1 deletion rational.html
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ <h2><a name="Class rational synopsis">Class rational synopsis</a></h2>

// Input and output
template &lt;typename I&gt; std::istream&amp; operator&gt;&gt; (std::istream&amp; is, rational&lt;I&gt;&amp; r);
template &lt;typename I&gt; std::ostream&amp; operator&lt;&lt; (std::ostream&amp; os, const rational&lt;I&gt;&amp; r);
template &lt;typename I, typename Ch, typename Tr&gt;
std::basic_ostream&lt;Ch, Tr&gt;&amp; operator&lt;&lt; (std::basic_ostream&lt;Ch, Tr&gt;&amp; os, const rational&lt;I&gt;&amp; r);

// Type conversion
template &lt;typename T, typename I&gt; T rational_cast (const rational&lt;I&gt;&amp; r); // constexpr since C++11
Expand Down
37 changes: 37 additions & 0 deletions test/rational_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,43 @@ BOOST_AUTO_TEST_CASE_TEMPLATE( rational_output_test, T, all_signed_test_types )
BOOST_CHECK_EQUAL( oss.str(), "0x22/3db" ); // not "0x22/0x3db"
}

#if !defined(BOOST_NO_STD_WSTRING) && !defined(BOOST_NO_STD_WSTREAMBUF)
BOOST_AUTO_TEST_CASE_TEMPLATE( rational_wide_output_test, T,
builtin_signed_test_types )
{
using namespace std;
typedef boost::rational<T> rational_type;

wostringstream oss;

oss << rational_type( 44, 14 );
BOOST_CHECK( oss.str() == L"22/7" );

// Rational output must not fall back to Boolean conversion.
oss.clear(); oss.str( L"" );
oss << boolalpha << rational_type( 0 ) << L' ' << rational_type( 1, 3 );
BOOST_CHECK( oss.str() == L"0/1 1/3" );

oss.clear(); oss.str( L"" );
oss << setw( 5 ) << setfill(L'*') << rational_type( 1, 2 ) << L'r';
BOOST_CHECK( oss.str() == L"**1/2r" );
BOOST_CHECK_EQUAL( oss.width(), 0 );
BOOST_CHECK_EQUAL( oss.fill(), L'*' );

oss.clear(); oss.str( L"" );
oss << showpos << rational_type( 2, 3 ) << noshowpos;
BOOST_CHECK( oss.str() == L"+2/3" );

oss.clear(); oss.str( L"" );
oss << setw( 8 ) << internal << rational_type( 36, -15 ) << right << L'r';
BOOST_CHECK( oss.str() == L"-***12/5r" );

oss.clear(); oss.str( L"" );
oss << showbase << hex << rational_type( 34, 987 ) << noshowbase << dec;
BOOST_CHECK( oss.str() == L"0x22/3db" );
}
#endif

// Input test, failing
BOOST_AUTO_TEST_CASE_TEMPLATE( rational_input_failing_test, T,
all_signed_test_types )
Expand Down