2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // Official repository: https://github.com/boostorg/json
10 #ifndef BOOST_JSON_PILFER_HPP
11 #define BOOST_JSON_PILFER_HPP
13 #include <boost/json/detail/config.hpp>
14 #include <type_traits>
18 Implements "pilfering" from P0308R0
21 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0308r0.html
27 /** Tag wrapper to specify pilfer-construction.
29 This wrapper is used to specify a pilfer constructor
34 A pilfer constructor accepts a single argument
35 of type @ref pilfered and throws nothing:
40 T( pilfered<T> ) noexcept;
46 The constructor should not be marked explicit.
48 @see @ref pilfer, @ref is_pilfer_constructible,
49 <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0308r0.html">
50 Valueless Variants Considered Harmful</a>
60 Construct the wrapper from `t`.
62 @param t The pilferable object. Ownership
67 pilfered(T&& t) noexcept
72 /** Return a reference to the pilferable object.
74 This returns a reference to the wrapped object.
82 /** Return a pointer to the pilferable object.
84 This returns a pointer to the wrapped object.
87 operator->() const noexcept
89 //return std::addressof(t_);
90 return reinterpret_cast<T*>(
93 const volatile char &>(t_)));
97 #ifndef BOOST_JSON_DOCS
98 // VFALCO Renamed this to work around an msvc bug
99 namespace detail_pilfer {
107 /** Metafunction returning `true` if `T` is <em>PilferConstructible</em>
109 If `T` can be pilfer constructed, this metafunction is
110 equal to `std::true_type`. Otherwise it is equal to
113 @see @ref pilfer, @ref pilfered,
114 <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0308r0.html">
115 Valueless Variants Considered Harmful</a>
118 struct is_pilfer_constructible
119 #ifndef BOOST_JSON_DOCS
120 : std::integral_constant<bool,
121 std::is_nothrow_move_constructible<T>::value ||
123 std::is_nothrow_constructible<
124 T, pilfered<T> >::value &&
125 ! std::is_nothrow_constructible<
126 T, detail_pilfer::not_pilfered<T> >::value
132 /** Indicate that an object `t` may be pilfered from.
134 A <em>pilfer</em> operation is the construction
135 of a new object of type `T` from an existing
136 object `t`. After the construction, the only
137 valid operation on the pilfered-from object is
138 destruction. This permits optimizations beyond
139 those available for a move-construction, as the
140 pilfered-from object is not required to be in
143 This is used similarly to `std::move`.
147 A pilfer constructor accepts a single argument
148 of type @ref pilfered and throws nothing:
153 T( pilfered<T> ) noexcept;
157 Pilfer construction is performed using @ref pilfer :
161 T t1; // default construction
162 T t2( pilfer( t1 ) ); // pilfer-construct from t1
164 // At this point, t1 may only be destroyed
168 @see @ref pilfered, @ref is_pilfer_constructible,
169 <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0308r0.html">
170 Valueless Variants Considered Harmful</a>
174 pilfer(T&& t) noexcept ->
175 typename std::conditional<
176 std::is_nothrow_constructible<
177 typename std::remove_reference<T>::type,
179 std::remove_reference<T>::type> >::value &&
180 ! std::is_nothrow_constructible<
181 typename std::remove_reference<T>::type,
182 detail_pilfer::not_pilfered<typename
183 std::remove_reference<T>::type> >::value,
184 pilfered<typename std::remove_reference<T>::type>,
185 typename std::remove_reference<T>::type&&
189 typename std::remove_reference<T>::type;
191 is_pilfer_constructible<U>::value, "");
192 return typename std::conditional<
193 std::is_nothrow_constructible<
194 U, pilfered<U> >::value &&
195 ! std::is_nothrow_constructible<
196 U, detail_pilfer::not_pilfered<U> >::value,
198 >::type(std::move(t));
204 relocate(T* dest, T& src) noexcept
207 is_pilfer_constructible<T>::value, "");
208 ::new(dest) T(pilfer(src));