boost::none - simpler and works with MSVC

This commit is contained in:
Andrzej Krzemienski
2015-10-01 15:26:15 +02:00
parent 593710e961
commit 9f8dd57386
8 changed files with 66 additions and 18 deletions

View File

@ -15,14 +15,16 @@
```
namespace boost {
class none_t {};
class none_t {/* see below */};
extern const none_t none; // see below
const none_t none (/* see below */);
} // namespace boost
```
Variable `none` has external linkage, however it is not required to link with any library to obtain its definition. Only by including this header file, the definition becomes available, by means of using template instantiation.
Class `none_t` is meant to serve as a tag for selecting appropriate overloads of from `optional`'s interface. It is an empty, trivially copyable class with disabled default constructor.
Constant `none` is used to indicate an optional object that does not contain a value in initialization, assignment and relational operations of `optional`.
[endsect]

View File

@ -146,7 +146,7 @@
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"><p><small>Last revised: July 08, 2015 at 21:39:55 GMT</small></p></td>
<td align="left"><p><small>Last revised: October 01, 2015 at 13:19:55 GMT</small></p></td>
<td align="right"><div class="copyright-footer"></div></td>
</tr></table>
<hr>

View File

@ -52,19 +52,23 @@
</p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span>
<span class="keyword">class</span> <span class="identifier">none_t</span> <span class="special">{};</span>
<span class="keyword">class</span> <span class="identifier">none_t</span> <span class="special">{/*</span> <span class="identifier">see</span> <span class="identifier">below</span> <span class="special">*/};</span>
<span class="keyword">extern</span> <span class="keyword">const</span> <span class="identifier">none_t</span> <span class="identifier">none</span><span class="special">;</span> <span class="comment">// see below</span>
<span class="keyword">const</span> <span class="identifier">none_t</span> <span class="identifier">none</span> <span class="special">(/*</span> <span class="identifier">see</span> <span class="identifier">below</span> <span class="special">*/);</span>
<span class="special">}</span> <span class="comment">// namespace boost</span>
</pre>
<p>
</p>
<p>
Variable <code class="computeroutput"><span class="identifier">none</span></code> has external
linkage, however it is not required to link with any library to obtain
its definition. Only by including this header file, the definition becomes
available, by means of using template instantiation.
Class <code class="computeroutput"><span class="identifier">none_t</span></code> is meant to
serve as a tag for selecting appropriate overloads of from <code class="computeroutput"><span class="identifier">optional</span></code>'s interface. It is an empty,
trivially copyable class with disabled default constructor.
</p>
<p>
Constant <code class="computeroutput"><span class="identifier">none</span></code> is used to
indicate an optional object that does not contain a value in initialization,
assignment and relational operations of <code class="computeroutput"><span class="identifier">optional</span></code>.
</p>
</div>
</div>

View File

@ -22,8 +22,10 @@
namespace boost {
#ifdef BOOST_OPTIONAL_USE_OLD_DEFINITION_OF_NONE
none_t const none = (static_cast<none_t>(0)) ;
#else
#elif defined BOOST_OPTIONAL_USE_SINGLETON_DEFINITION_OF_NONE
namespace detail { namespace optional_detail {
@ -45,9 +47,13 @@ namespace {
const none_t& none = detail::optional_detail::none_instance<none_t>::instance;
}
#endif
#else
const none_t none ((none_t::init_tag()));
#endif // older definitions
} // namespace boost
#endif
#endif // header guard

View File

@ -16,13 +16,25 @@
namespace boost {
#ifdef BOOST_OPTIONAL_USE_OLD_DEFINITION_OF_NONE
namespace detail { struct none_helper{}; }
typedef int detail::none_helper::*none_t ;
#else
#elif defined BOOST_OPTIONAL_USE_SINGLETON_DEFINITION_OF_NONE
class none_t {};
#endif
#else
struct none_t
{
struct init_tag{};
explicit none_t(init_tag){} // to prevent default constructor
};
#endif // old implementation workarounds
} // namespace boost
#endif
#endif // header guard

View File

@ -15,7 +15,7 @@
#include <istream>
#include <ostream>
#include <boost/none.hpp>
#include "boost/none.hpp"
#include "boost/optional/optional.hpp"
@ -25,7 +25,7 @@ namespace boost
template<class CharType, class CharTrait>
inline
std::basic_ostream<CharType, CharTrait>&
operator<<(std::basic_ostream<CharType, CharTrait>& out, none_t const&)
operator<<(std::basic_ostream<CharType, CharTrait>& out, none_t)
{
if (out.good())
{

View File

@ -58,6 +58,7 @@ import testing ;
[ compile-fail optional_test_fail_explicit_convert_in_value_or.cpp ]
[ compile-fail optional_test_fail_explicit_convert_in_value_or_call.cpp ]
[ compile-fail optional_test_fail_io_without_io.cpp ]
[ compile-fail optional_test_fail_none_io_without_io.cpp ]
[ compile-fail optional_test_fail_convert_assign_of_enums.cpp ]
;
}

View File

@ -0,0 +1,23 @@
// Copyright (C) 2015, Andrzej Krzemienski.
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/lib/optional for documentation.
//
// You are welcome to contact the author at:
// akrzemi1@gmail.com
#include <iostream>
#include "boost/none.hpp"
// but no boost/optional/optional_io.hpp
// THIS TEST SHOULD FAIL TO COMPILE
// Unless one includes header boost/optional/optional_io.hpp, it should not be possible
// to stream out boost::none.
void test_streaming_out_none()
{
std::cout << boost::none;
}