added VERIFY in test and changed the template parameter naming. diff --git a/libstdc++-v3/include/std/any b/libstdc++-v3/include/std/any index 6b7e68f0e63..d350d0b2575 100644 --- a/libstdc++-v3/include/std/any +++ b/libstdc++-v3/include/std/any @@ -176,36 +176,23 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION typename __any_constructible<bool, _Tp, _Args...>::type;
/// Construct with a copy of @p __value as the contained object. - template <typename _ValueType, typename _Tp = _Decay<_ValueType>, - typename _Mgr = _Manager<_Tp>, - __any_constructible_t<_Tp, _ValueType&&> = true, - enable_if_t<!__is_in_place_type<_Tp>::value, bool> = true> - any(_ValueType&& __value) + template <typename _Tp, typename _VTp = _Decay<_Tp>, + typename _Mgr = _Manager<_VTp>, + enable_if_t<is_copy_constructible<_VTp>::value && + !__is_in_place_type<_VTp>::value, bool> = true> + any(_Tp&& __value) : _M_manager(&_Mgr::_S_manage) { - _Mgr::_S_create(_M_storage, std::forward<_ValueType>(__value)); - } - - /// Construct with a copy of @p __value as the contained object. - template <typename _ValueType, typename _Tp = _Decay<_ValueType>, - typename _Mgr = _Manager<_Tp>, - enable_if_t<__and_v<is_copy_constructible<_Tp>, - __not_<is_constructible<_Tp, _ValueType&&>>, - __not_<__is_in_place_type<_Tp>>>, - bool> = false> - any(_ValueType&& __value) - : _M_manager(&_Mgr::_S_manage) - { - _Mgr::_S_create(_M_storage, __value); + _Mgr::_S_create(_M_storage, std::forward<_Tp>(__value)); } /// Construct with an object created from @p __args as the contained object. - template <typename _ValueType, typename... _Args, - typename _Tp = _Decay<_ValueType>, - typename _Mgr = _Manager<_Tp>, - __any_constructible_t<_Tp, _Args&&...> = false> + template <typename _Tp, typename... _Args, + typename _VTp = decay_t<_Tp>, + typename _Mgr = _Manager<_VTp>, + __any_constructible_t<_VTp, _Args&&...> = false> explicit - any(in_place_type_t<_ValueType>, _Args&&... __args) + any(in_place_type_t<_Tp>, _Args&&... __args) : _M_manager(&_Mgr::_S_manage) { _Mgr::_S_create(_M_storage, std::forward<_Args>(__args)...); @@ -213,13 +200,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION /// Construct with an object created from @p __il and @p __args as /// the contained object. - template <typename _ValueType, typename _Up, typename... _Args, - typename _Tp = _Decay<_ValueType>, - typename _Mgr = _Manager<_Tp>, - __any_constructible_t<_Tp, initializer_list<_Up>, + template <typename _Tp, typename _Up, typename... _Args, + typename _VTp = decay_t<_Tp>, + typename _Mgr = _Manager<_VTp>, + __any_constructible_t<_VTp, initializer_list<_Up>, _Args&&...> = false> explicit - any(in_place_type_t<_ValueType>, + any(in_place_type_t<_Tp>, initializer_list<_Up> __il, _Args&&... __args) : _M_manager(&_Mgr::_S_manage) { @@ -258,40 +245,43 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } /// Store a copy of @p __rhs as the contained object. - template<typename _ValueType> - enable_if_t<is_copy_constructible<_Decay<_ValueType>>::value, any&> - operator=(_ValueType&& __rhs) + template<typename _Tp> + enable_if_t<is_copy_constructible<_Decay<_Tp>>::value, any&> + operator=(_Tp&& __rhs) { - *this = any(std::forward<_ValueType>(__rhs)); + *this = any(std::forward<_Tp>(__rhs)); return *this; } /// Emplace with an object created from @p __args as the contained object. - template <typename _ValueType, typename... _Args> - typename __any_constructible<_Decay<_ValueType>&, - _Decay<_ValueType>, _Args&&...>::type + template <typename _Tp, + typename... _Args, typename _VTp = decay_t<_Tp>> + typename __any_constructible<_VTp&, + _VTp, _Args&&...>::type emplace(_Args&&... __args) { - __do_emplace<_Decay<_ValueType>>(std::forward<_Args>(__args)...); + __do_emplace<_VTp>(std::forward<_Args>(__args)...); any::_Arg __arg; this->_M_manager(any::_Op_access, this, &__arg); - return *static_cast<_Decay<_ValueType>*>(__arg._M_obj); + return *static_cast<_VTp*>(__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<_Decay<_ValueType>&, - _Decay<_ValueType>, + template <typename _Tp, + typename _Up, typename... _Args, + typename _VTp = decay_t<_Tp>> + typename __any_constructible<_VTp&, + _VTp, initializer_list<_Up>, _Args&&...>::type emplace(initializer_list<_Up> __il, _Args&&... __args) { - __do_emplace<_Decay<_ValueType>, _Up>(__il, + __do_emplace<_VTp, _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); + return *static_cast<_VTp*>(__arg._M_obj); } // modifiers diff --git a/libstdc++-v3/testsuite/20_util/any/misc/92156.cc b/libstdc++-v3/testsuite/20_util/any/misc/92156.cc new file mode 100644 index 00000000000..df6c9deff1b --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/any/misc/92156.cc @@ -0,0 +1,39 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2014-2020 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <any> +#include <utility> +#include <tuple> +#include <type_traits> +#include <testsuite_hooks.h> + + +int main() { + auto a = std::any(std::in_place_type<std::any>, 5); + VERIFY(std::any_cast<int>(a) == 5); + auto b = std::any(std::in_place_type<std::any>, {1}); + VERIFY(std::any_cast<int>(b) == 1); + std::any p = std::pair<std::any, std::any>(1, 1); + VERIFY((std::any_cast<std::pair<int,int>>(p) == std::pair<int,int>(1,1))); + std::any t = std::tuple<std::any>(1); + VERIFY((std::any_cast<std::tuple<int>>(t) == std::tuple<int>(1))); + return 0; +} + thanks, On Tue, Apr 21, 2020 at 5:14 PM Ville Voutilainen <ville.voutilai...@gmail.com> wrote: > > On Tue, 21 Apr 2020 at 11:29, kamlesh kumar <kamleshbha...@gmail.com> wrote: > > > > Added the fix for emplace. > > > > diff --git a/libstdc++-v3/include/std/any b/libstdc++-v3/include/std/any > > index 6b7e68f0e63..f35d90e548d 100644 > > --- a/libstdc++-v3/include/std/any > > +++ b/libstdc++-v3/include/std/any > > @@ -178,30 +178,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION > > /// Construct with a copy of @p __value as the contained object. > > template <typename _ValueType, typename _Tp = _Decay<_ValueType>, > > While we're at it, we should rename _ValueType to _Tp and the decayed > type to _VTp, > so that it matches the standard's naming as close as possible, and > thus removes the ongoing > maintenance confusion about which is which. > > > +int main() { > > + auto a = std::any(std::in_place_type<std::any>, 5); > > + auto b = std::any(std::in_place_type<std::any>, {1}); > > + std::any p = std::pair<std::any, std::any>(1, 1); > > + (void)p; > > + std::any t = std::tuple<std::any>(1); > > I think this sort of tests should VERIFY that the constructed any > contains what we expect. > Iow, do an any_cast and check that, for instance, a and b contain an any.