mirror of
https://github.com/boostorg/optional.git
synced 2025-07-23 09:07:17 +02:00
Improvement to "cout << none" implementation
This commit is contained in:
@ -28,8 +28,12 @@ inline
|
|||||||
std::basic_ostream<CharType, CharTrait>&
|
std::basic_ostream<CharType, CharTrait>&
|
||||||
operator<<(std::basic_ostream<CharType, CharTrait>& out, none_t const&)
|
operator<<(std::basic_ostream<CharType, CharTrait>& out, none_t const&)
|
||||||
{
|
{
|
||||||
|
if (out.good())
|
||||||
|
{
|
||||||
out << "--";
|
out << "--";
|
||||||
return out;
|
}
|
||||||
|
|
||||||
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class CharType, class CharTrait, class T>
|
template<class CharType, class CharTrait, class T>
|
||||||
@ -37,9 +41,9 @@ inline
|
|||||||
std::basic_ostream<CharType, CharTrait>&
|
std::basic_ostream<CharType, CharTrait>&
|
||||||
operator<<(std::basic_ostream<CharType, CharTrait>& out, optional<T> const& v)
|
operator<<(std::basic_ostream<CharType, CharTrait>& out, optional<T> const& v)
|
||||||
{
|
{
|
||||||
if ( out.good() )
|
if (out.good())
|
||||||
{
|
{
|
||||||
if ( !v )
|
if (!v)
|
||||||
out << "--" ;
|
out << "--" ;
|
||||||
else out << ' ' << *v ;
|
else out << ' ' << *v ;
|
||||||
}
|
}
|
||||||
|
@ -61,8 +61,8 @@ void test2( Opt o, Opt buff )
|
|||||||
s << o << " " << markv ;
|
s << o << " " << markv ;
|
||||||
s >> buff >> mark ;
|
s >> buff >> mark ;
|
||||||
|
|
||||||
BOOST_ASSERT( buff == o ) ;
|
BOOST_CHECK( buff == o ) ;
|
||||||
BOOST_ASSERT( mark == markv ) ;
|
BOOST_CHECK( mark == markv ) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -75,12 +75,32 @@ void test( T v, T w )
|
|||||||
test2( optional<T> () , make_optional(w));
|
test2( optional<T> () , make_optional(w));
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
test()
|
template <class T>
|
||||||
|
void subtest_tag_none_reversibility_with_optional(optional<T> ov)
|
||||||
{
|
{
|
||||||
stringstream s ;
|
stringstream s;
|
||||||
s << boost::none;
|
s << boost::none;
|
||||||
BOOST_ASSERT(s.str() == "--");
|
s >> ov;
|
||||||
|
BOOST_CHECK(!ov);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void subtest_tag_none_equivalence_with_optional()
|
||||||
|
{
|
||||||
|
stringstream s, r;
|
||||||
|
optional<T> ov;
|
||||||
|
s << boost::none;
|
||||||
|
r << ov;
|
||||||
|
BOOST_CHECK(s.str() == r.str());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void test_tag_none(T v)
|
||||||
|
{
|
||||||
|
subtest_tag_none_reversibility_with_optional(optional<T>(v));
|
||||||
|
subtest_tag_none_reversibility_with_optional(optional<T>());
|
||||||
|
subtest_tag_none_equivalence_with_optional<T>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -90,7 +110,8 @@ int test_main( int, char* [] )
|
|||||||
{
|
{
|
||||||
test(1,2);
|
test(1,2);
|
||||||
test(string("hello"),string("buffer"));
|
test(string("hello"),string("buffer"));
|
||||||
test();
|
test_tag_none(10);
|
||||||
|
test_tag_none(string("text"));
|
||||||
}
|
}
|
||||||
catch ( ... )
|
catch ( ... )
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user