+2016-07-05 Ville Voutilainen <ville.voutilainen@gmail.com>
+
+ Implement LWG 2509,
+ any_cast doesn't work with rvalue reference targets and cannot
+ move with a value target.
+ * include/experimental/any (any(_ValueType&&)): Constrain and
+ add an overload that doesn't forward.
+ (any_cast(any&&)): Constrain and add an overload that moves.
+ * testsuite/experimental/any/misc/any_cast.cc: Add tests for
+ the functionality added by LWG 2509.
+
2016-07-04 François Dumont <fdumont@gcc.gnu.org>
* testsuite/23_containers/vector/modifiers/emplace/self_emplace.cc:
/// Construct with a copy of @p __value as the contained object.
template <typename _ValueType, typename _Tp = _Decay<_ValueType>,
- typename _Mgr = _Manager<_Tp>>
+ typename _Mgr = _Manager<_Tp>,
+ typename enable_if<is_constructible<_Tp, _ValueType&&>::value,
+ bool>::type = true>
any(_ValueType&& __value)
: _M_manager(&_Mgr::_S_manage)
{
"The contained object must be CopyConstructible");
}
+ /// Construct with a copy of @p __value as the contained object.
+ template <typename _ValueType, typename _Tp = _Decay<_ValueType>,
+ typename _Mgr = _Manager<_Tp>,
+ typename enable_if<!is_constructible<_Tp, _ValueType&&>::value,
+ bool>::type = false>
+ any(_ValueType&& __value)
+ : _M_manager(&_Mgr::_S_manage)
+ {
+ _Mgr::_S_create(_M_storage, __value);
+ static_assert(is_copy_constructible<_Tp>::value,
+ "The contained object must be CopyConstructible");
+ }
+
/// Destructor, calls @c clear()
~any() { clear(); }
__throw_bad_any_cast();
}
- template<typename _ValueType>
+ template<typename _ValueType,
+ typename enable_if<!is_move_constructible<_ValueType>::value
+ || is_lvalue_reference<_ValueType>::value,
+ bool>::type = true>
inline _ValueType any_cast(any&& __any)
{
static_assert(any::__is_valid_cast<_ValueType>(),
return *__p;
__throw_bad_any_cast();
}
+
+ template<typename _ValueType,
+ typename enable_if<is_move_constructible<_ValueType>::value
+ && !is_lvalue_reference<_ValueType>::value,
+ bool>::type = false>
+ inline _ValueType any_cast(any&& __any)
+ {
+ static_assert(any::__is_valid_cast<_ValueType>(),
+ "Template argument must be a reference or CopyConstructible type");
+ auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
+ if (__p)
+ return std::move(*__p);
+ __throw_bad_any_cast();
+ }
// @}
template<typename _Tp>
}
}
+static int move_count = 0;
+
+void test03()
+{
+ struct MoveEnabled
+ {
+ MoveEnabled(MoveEnabled&&)
+ {
+ ++move_count;
+ }
+ MoveEnabled() = default;
+ MoveEnabled(const MoveEnabled&) = default;
+ };
+ MoveEnabled m;
+ MoveEnabled m2 = any_cast<MoveEnabled>(any(m));
+ VERIFY(move_count == 1);
+ MoveEnabled&& m3 = any_cast<MoveEnabled&&>(any(m));
+ VERIFY(move_count == 1);
+ struct MoveDeleted
+ {
+ MoveDeleted(MoveDeleted&&) = delete;
+ MoveDeleted() = default;
+ MoveDeleted(const MoveDeleted&) = default;
+ };
+ MoveDeleted md;
+ MoveDeleted&& md2 = any_cast<MoveDeleted>(any(std::move(md)));
+ MoveDeleted&& md3 = any_cast<MoveDeleted&&>(any(std::move(md)));
+}
+
int main()
{
test01();
test02();
+ test03();
}