diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2020-04-24 00:54:20 +0100 |
---|---|---|
committer | Jonathan Wakely <jwakely@redhat.com> | 2020-04-24 00:54:20 +0100 |
commit | d1462b0782555354b4480e1f46498586d5882972 (patch) | |
tree | 5e31e22610f4c0e69c465c88c1bf9dea4ef07d0c /libstdc++-v3/testsuite | |
parent | ae962e573ea5063fda7e86f93d9622e64cea9a7e (diff) |
libstdc++: Fix constructor constraints for std::any (PR 90415)
This removes a non-standard extension to std::any which causes errors
for valid code, due to recursive instantiation of a trait that isn't
supposed to be in the constraints.
It also removes some incorrect constraints on the in_place_type<T>
constructors and emplace members, which were preventing creating a
std::any object with another std::any as the contained value.
2020-04-24 Kamlesh Kumar <kamleshbhalui@gmail.com>
Jonathan Wakely <jwakely@redhat.com>
PR libstdc++/90415
PR libstdc++/92156
* include/std/any (any): Rename template parameters for consistency
with the standard.
(any::_Decay): Rename to _Decay_if_not_any.
(any::any(T&&):: Remove is_constructible from constraints. Remove
non-standard overload.
(any::any(in_place_type_t<T>, Args&&...))
(any::any(in_place_type_t<T>, initializer_list<U>, Args&&...))
(any::emplace(Args&&...))
(any::emplace(initializer_list<U>, Args&&...)):
Use decay_t instead of _Decay.
* testsuite/20_util/any/cons/90415.cc: New test.
* testsuite/20_util/any/cons/92156.cc: New Test.
* testsuite/20_util/any/misc/any_cast_neg.cc: Make dg-error directives
more robust.
* testsuite/20_util/any/modifiers/92156.cc: New test.
Diffstat (limited to 'libstdc++-v3/testsuite')
-rw-r--r-- | libstdc++-v3/testsuite/20_util/any/cons/90415.cc | 64 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/20_util/any/cons/92156.cc | 53 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/20_util/any/misc/any_cast_neg.cc | 16 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/20_util/any/modifiers/92156.cc | 57 |
4 files changed, 183 insertions, 7 deletions
diff --git a/libstdc++-v3/testsuite/20_util/any/cons/90415.cc b/libstdc++-v3/testsuite/20_util/any/cons/90415.cc new file mode 100644 index 00000000000..122262386d3 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/any/cons/90415.cc @@ -0,0 +1,64 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run { target c++17 } } + +// Copyright (C) 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 <testsuite_hooks.h> + +void +test01() +{ + // PR libstdc++/90415 + static_assert( std::is_copy_constructible<std::tuple<std::any>>::value ); +} + +struct wrapper +{ + wrapper() = default; + + wrapper(const std::any& t); + + wrapper(const wrapper& w); + + auto& operator=(const std::any& t); + + auto& operator=(const wrapper& w) + { + value = w.value; + return *this; + } + + std::any value; +}; + +void +test02() +{ + // PR libstdc++/91630 + wrapper a, b; + a = b; +} + +int main() +{ + test01(); + test02(); +} diff --git a/libstdc++-v3/testsuite/20_util/any/cons/92156.cc b/libstdc++-v3/testsuite/20_util/any/cons/92156.cc new file mode 100644 index 00000000000..d797473716d --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/any/cons/92156.cc @@ -0,0 +1,53 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run { target c++17 } } + +// Copyright (C) 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 <testsuite_hooks.h> + +void +test01() +{ + auto a = std::any(std::in_place_type<std::any>, 5); + VERIFY( std::any_cast<int>(std::any_cast<std::any>(a)) == 5 ); + + auto b = std::any(std::in_place_type<std::any>, {1}); + (void) std::any_cast<std::initializer_list<int>>(std::any_cast<std::any>(b)); +} + +void +test02() +{ + std::any p = std::pair<std::any, std::any>(1, 1); + auto pt = std::any_cast<std::pair<std::any, std::any>>(p); + VERIFY( std::any_cast<int>(pt.first) == 1 ); + VERIFY( std::any_cast<int>(pt.second) == 1 ); + + std::any t = std::tuple<std::any>(1); + auto tt = std::any_cast<std::tuple<std::any>>(t); + VERIFY( std::any_cast<int>(std::get<0>(tt)) == 1 ); +} + +int main() +{ + test01(); + test02(); +} diff --git a/libstdc++-v3/testsuite/20_util/any/misc/any_cast_neg.cc b/libstdc++-v3/testsuite/20_util/any/misc/any_cast_neg.cc index 6a271e8faab..049815a0c1f 100644 --- a/libstdc++-v3/testsuite/20_util/any/misc/any_cast_neg.cc +++ b/libstdc++-v3/testsuite/20_util/any/misc/any_cast_neg.cc @@ -1,5 +1,5 @@ // { dg-options "-std=gnu++17" } -// { dg-do compile } +// { dg-do compile { target c++17 } } // Copyright (C) 2014-2020 Free Software Foundation, Inc. // @@ -26,20 +26,22 @@ using std::any_cast; void test01() { const any y(1); - any_cast<int&>(y); // { dg-error "invalid 'static_cast'" "" { target { *-*-* } } 461 } - // { dg-error "Template argument must be constructible from a const value" "" { target { *-*-* } } 457 } + any_cast<int&>(y); // { dg-error "here" } + // { dg-error "Template argument must be constructible from a const value" "" { target { *-*-* } } 0 } } void test02() { any y(1); - any_cast<int&&>(y); - // { dg-error "Template argument must be constructible from an lvalue" "" { target { *-*-* } } 483 } + any_cast<int&&>(y); // { dg-error "here" } + // { dg-error "Template argument must be constructible from an lvalue" "" { target { *-*-* } } 0 } } void test03() { any y(1); - any_cast<int&>(std::move(y)); // { dg-error "invalid 'static_cast'" "" { target { *-*-* } } 501 } - // { dg-error "Template argument must be constructible from an rvalue" "" { target { *-*-* } } 497 } + any_cast<int&>(std::move(y)); // { dg-error "here" } + // { dg-error "Template argument must be constructible from an rvalue" "" { target { *-*-* } } 0 } } + +// { dg-prune-output "invalid 'static_cast'" } diff --git a/libstdc++-v3/testsuite/20_util/any/modifiers/92156.cc b/libstdc++-v3/testsuite/20_util/any/modifiers/92156.cc new file mode 100644 index 00000000000..4a7bc97bb83 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/any/modifiers/92156.cc @@ -0,0 +1,57 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run { target c++17 } } + +// Copyright (C) 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 <testsuite_hooks.h> + +void +test01() +{ + std::any a; + a.emplace<std::any>(5); + VERIFY( std::any_cast<int>(std::any_cast<std::any>(a)) == 5 ); + + std::any b; + b.emplace<std::any>({1}); + (void) std::any_cast<std::initializer_list<int>>(std::any_cast<std::any>(b)); +} + +void +test02() +{ + std::any p; + p.emplace<std::pair<std::any, std::any>>(1, 1); + auto pt = std::any_cast<std::pair<std::any, std::any>>(p); + VERIFY( std::any_cast<int>(pt.first) == 1 ); + VERIFY( std::any_cast<int>(pt.second) == 1 ); + + std::any t; + t.emplace<std::tuple<std::any>>(1); + auto tt = std::any_cast<std::tuple<std::any>>(t); + VERIFY( std::any_cast<int>(std::get<0>(tt)) == 1 ); +} + +int main() +{ + test01(); + test02(); +} |