mirror of
https://github.com/boostorg/optional.git
synced 2025-07-15 13:26:37 +02:00
boost::none - simpler and works with MSVC
This commit is contained in:
@ -15,14 +15,16 @@
|
|||||||
```
|
```
|
||||||
namespace boost {
|
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
|
} // 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]
|
[endsect]
|
||||||
|
|
||||||
|
@ -146,7 +146,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
<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>
|
<td align="right"><div class="copyright-footer"></div></td>
|
||||||
</tr></table>
|
</tr></table>
|
||||||
<hr>
|
<hr>
|
||||||
|
@ -52,19 +52,23 @@
|
|||||||
</p>
|
</p>
|
||||||
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span>
|
<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>
|
<span class="special">}</span> <span class="comment">// namespace boost</span>
|
||||||
</pre>
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
Variable <code class="computeroutput"><span class="identifier">none</span></code> has external
|
Class <code class="computeroutput"><span class="identifier">none_t</span></code> is meant to
|
||||||
linkage, however it is not required to link with any library to obtain
|
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,
|
||||||
its definition. Only by including this header file, the definition becomes
|
trivially copyable class with disabled default constructor.
|
||||||
available, by means of using template instantiation.
|
</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>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -22,8 +22,10 @@
|
|||||||
namespace boost {
|
namespace boost {
|
||||||
|
|
||||||
#ifdef BOOST_OPTIONAL_USE_OLD_DEFINITION_OF_NONE
|
#ifdef BOOST_OPTIONAL_USE_OLD_DEFINITION_OF_NONE
|
||||||
|
|
||||||
none_t const none = (static_cast<none_t>(0)) ;
|
none_t const none = (static_cast<none_t>(0)) ;
|
||||||
#else
|
|
||||||
|
#elif defined BOOST_OPTIONAL_USE_SINGLETON_DEFINITION_OF_NONE
|
||||||
|
|
||||||
namespace detail { namespace optional_detail {
|
namespace detail { namespace optional_detail {
|
||||||
|
|
||||||
@ -45,9 +47,13 @@ namespace {
|
|||||||
const none_t& none = detail::optional_detail::none_instance<none_t>::instance;
|
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
|
} // namespace boost
|
||||||
|
|
||||||
#endif
|
#endif // header guard
|
||||||
|
|
||||||
|
@ -16,13 +16,25 @@
|
|||||||
namespace boost {
|
namespace boost {
|
||||||
|
|
||||||
#ifdef BOOST_OPTIONAL_USE_OLD_DEFINITION_OF_NONE
|
#ifdef BOOST_OPTIONAL_USE_OLD_DEFINITION_OF_NONE
|
||||||
|
|
||||||
namespace detail { struct none_helper{}; }
|
namespace detail { struct none_helper{}; }
|
||||||
typedef int detail::none_helper::*none_t ;
|
typedef int detail::none_helper::*none_t ;
|
||||||
#else
|
|
||||||
|
#elif defined BOOST_OPTIONAL_USE_SINGLETON_DEFINITION_OF_NONE
|
||||||
|
|
||||||
class none_t {};
|
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
|
} // namespace boost
|
||||||
|
|
||||||
#endif
|
#endif // header guard
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
#include <istream>
|
#include <istream>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
|
||||||
#include <boost/none.hpp>
|
#include "boost/none.hpp"
|
||||||
#include "boost/optional/optional.hpp"
|
#include "boost/optional/optional.hpp"
|
||||||
|
|
||||||
|
|
||||||
@ -25,7 +25,7 @@ namespace boost
|
|||||||
template<class CharType, class CharTrait>
|
template<class CharType, class CharTrait>
|
||||||
inline
|
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)
|
||||||
{
|
{
|
||||||
if (out.good())
|
if (out.good())
|
||||||
{
|
{
|
||||||
|
@ -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.cpp ]
|
||||||
[ compile-fail optional_test_fail_explicit_convert_in_value_or_call.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_io_without_io.cpp ]
|
||||||
|
[ compile-fail optional_test_fail_none_io_without_io.cpp ]
|
||||||
[ compile-fail optional_test_fail_convert_assign_of_enums.cpp ]
|
[ compile-fail optional_test_fail_convert_assign_of_enums.cpp ]
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
23
test/optional_test_fail_none_io_without_io.cpp
Normal file
23
test/optional_test_fail_none_io_without_io.cpp
Normal 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;
|
||||||
|
}
|
Reference in New Issue
Block a user