+2017-03-15 Ville Voutilainen <ville.voutilainen@gmail.com>
+
+ Implement LWG 2857, {variant,optional,any}::emplace should
+ return the constructed value.
+ * include/std/any (emplace(_Args&&...)): Change the return type and
+ return a reference to the constructed value.
+ (emplace(initializer_list<_Up>, _Args&&...)): Likewise.
+ * include/std/optional (emplace(_Args&&...)): Likewise.
+ (emplace(initializer_list<_Up>, _Args&&...)): Likewise.
+ * include/std/variant (emplace<_Tp>(_Args&&...)): Likewise.
+ (emplace<_Tp>(initializer_list<_Up>, _Args&&...)): Likewise.
+ (emplace<_Np>(_Args&&...)): Likewise.
+ (emplace<_Np>(initializer_list<_Up>, _Args&&...)): Likewise.
+ * testsuite/20_util/any/assign/emplace.cc: Add tests for
+ checking the return value of emplace.
+ * testsuite/20_util/any/misc/any_cast_neg.cc: Adjust.
+ * testsuite/20_util/optional/assignment/6.cc: Add tests for
+ checking the return value of emplace.
+ * testsuite/20_util/variant/run.cc: Likewise.
+
2017-03-15 Xi Ruoyao <ryxi@stu.xidian.edu.cn>
PR libstdc++/62045
/// Emplace with an object created from @p __args as the contained object.
template <typename _ValueType, typename... _Args>
- typename __any_constructible<void,
+ typename __any_constructible<_Decay<_ValueType>&,
_Decay<_ValueType>, _Args&&...>::type
emplace(_Args&&... __args)
{
__do_emplace<_Decay<_ValueType>>
(std::forward<_Args>(__args)...);
+ any::_Arg __arg;
+ this->_M_manager(any::_Op_access, this, &__arg);
+ return *static_cast<_Decay<_ValueType>*>(__arg._M_obj);
}
/// Emplace with an object created from @p __il and @p __args as
/// the contained object.
template <typename _ValueType, typename _Up, typename... _Args>
- typename __any_constructible<void,
+ typename __any_constructible<_Decay<_ValueType>&,
_Decay<_ValueType>,
initializer_list<_Up>,
_Args&&...>::type
{
__do_emplace<_Decay<_ValueType>, _Up>
(__il, std::forward<_Args>(__args)...);
+ any::_Arg __arg;
+ this->_M_manager(any::_Op_access, this, &__arg);
+ return *static_cast<_Decay<_ValueType>*>(__arg._M_obj);
}
// modifiers
}
template<typename... _Args>
- enable_if_t<is_constructible<_Tp, _Args&&...>::value>
+ enable_if_t<is_constructible<_Tp, _Args&&...>::value, _Tp&>
emplace(_Args&&... __args)
{
this->_M_reset();
this->_M_construct(std::forward<_Args>(__args)...);
+ return this->_M_get();
}
template<typename _Up, typename... _Args>
enable_if_t<is_constructible<_Tp, initializer_list<_Up>&,
- _Args&&...>::value>
+ _Args&&...>::value, _Tp&>
emplace(initializer_list<_Up> __il, _Args&&... __args)
{
this->_M_reset();
this->_M_construct(__il, std::forward<_Args>(__args)...);
+ return this->_M_get();
}
// Destructor is implicit, implemented in _Optional_base.
}
template<typename _Tp, typename... _Args>
- enable_if_t<is_constructible_v<_Tp, _Args...> && __exactly_once<_Tp>>
+ enable_if_t<is_constructible_v<_Tp, _Args...> && __exactly_once<_Tp>,
+ _Tp&>
emplace(_Args&&... __args)
{
- this->emplace<__index_of<_Tp>>(std::forward<_Args>(__args)...);
+ auto& ret =
+ this->emplace<__index_of<_Tp>>(std::forward<_Args>(__args)...);
__glibcxx_assert(holds_alternative<_Tp>(*this));
+ return ret;
}
template<typename _Tp, typename _Up, typename... _Args>
enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
- && __exactly_once<_Tp>>
+ && __exactly_once<_Tp>,
+ _Tp&>
emplace(initializer_list<_Up> __il, _Args&&... __args)
{
- this->emplace<__index_of<_Tp>>(__il, std::forward<_Args>(__args)...);
+ auto& ret =
+ this->emplace<__index_of<_Tp>>(__il,
+ std::forward<_Args>(__args)...);
__glibcxx_assert(holds_alternative<_Tp>(*this));
+ return ret;
}
template<size_t _Np, typename... _Args>
enable_if_t<is_constructible_v<variant_alternative_t<_Np, variant>,
- _Args...>>
+ _Args...>,
+ variant_alternative_t<_Np, variant>&>
emplace(_Args&&... __args)
{
static_assert(_Np < sizeof...(_Types),
__throw_exception_again;
}
__glibcxx_assert(index() == _Np);
+ return std::get<_Np>(*this);
}
template<size_t _Np, typename _Up, typename... _Args>
enable_if_t<is_constructible_v<variant_alternative_t<_Np, variant>,
- initializer_list<_Up>&, _Args...>>
+ initializer_list<_Up>&, _Args...>,
+ variant_alternative_t<_Np, variant>&>
emplace(initializer_list<_Up> __il, _Args&&... __args)
{
static_assert(_Np < sizeof...(_Types),
__throw_exception_again;
}
__glibcxx_assert(index() == _Np);
+ return std::get<_Np>(*this);
}
constexpr bool valueless_by_exception() const noexcept
std::any o10;
o10.emplace<char*>(nullptr);
VERIFY(o9.type() == o10.type());
+ std::any o11;
+ VERIFY(&o11.emplace<int>(42) == &std::any_cast<int&>(o11));
+ VERIFY(&o11.emplace<std::vector<int>>({1,2,3}) ==
+ &std::any_cast<std::vector<int>&>(o11));
}
using std::any_cast;
const any y(1);
- any_cast<int&>(y); // { dg-error "invalid static_cast" "" { target { *-*-* } } 455 }
+ any_cast<int&>(y); // { dg-error "invalid static_cast" "" { target { *-*-* } } 461 }
}
o.emplace({ 'a' }, "");
VERIFY( o && o->state == 2 );
}
+ {
+ O o;
+ VERIFY(&o.emplace(0) == &*o);
+ VERIFY(&o.emplace({ 'a' }, "") == &*o);
+ }
static_assert( !std::is_constructible<O, std::initializer_list<int>, int>(), "" );
try { v.emplace<1>(AlwaysThrow{}); } catch (nullptr_t) { }
VERIFY(v.valueless_by_exception());
}
+ VERIFY(&v.emplace<0>(1) == &std::get<0>(v));
+ VERIFY(&v.emplace<int>(1) == &std::get<int>(v));
+ VERIFY(&v.emplace<1>("a") == &std::get<1>(v));
+ VERIFY(&v.emplace<string>("a") == &std::get<string>(v));
+ {
+ variant<vector<int>> v;
+ VERIFY(&v.emplace<0>({1,2,3}) == &std::get<0>(v));
+ VERIFY(&v.emplace<vector<int>>({1,2,3}) == &std::get<vector<int>>(v));
+ }
}
void test_get()