-
Notifications
You must be signed in to change notification settings - Fork 1
/
required.hpp
59 lines (49 loc) · 1.24 KB
/
required.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright (C) 2013 Martin Moene.
//
// Implementation note: this version targets C++03 and C++11.
//
// 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)
//
// Based on exploding return codes by Ken Hagan.
// https://groups.google.com/forum/#!msg/comp.lang.c++.moderated/BkZqPfoq3ys/H_PMR8Sat4oJ
#ifndef NONSTD_REQUIRED_H_INCLUDED
#define NONSTD_REQUIRED_H_INCLUDED
namespace nonstd {
template <typename T>
class required
{
public:
typedef T value_type;
#if __cplusplus >= 201103L
~required() noexcept(false)
#else
~required()
#endif
{
if ( m_ignored )
throw m_value;
}
required( value_type const & v )
: m_value( v )
, m_ignored( true )
{}
// move "ignored" state unconditionally, C++03, C++11
required( required const & other )
: m_value( other.m_value )
, m_ignored( other.m_ignored )
{
other.m_ignored = false;
}
operator value_type() const
{
m_ignored = false;
return m_value;
}
private:
value_type m_value;
mutable bool m_ignored;
};
} // namespace nonstd
#endif // NONSTD_REQUIRED_H_INCLUDED