From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 5C0493858D3C; Wed, 29 Mar 2023 23:38:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5C0493858D3C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1680133111; bh=2dVuLKAl7mp0Y9rX6Rf6JlLxsUtKBY1qXIcWwSyxUgY=; h=From:To:Subject:Date:From; b=Sly61AsxhdcP878dld0ZsW0QPGT111Ms0ecuQy55gWtH+qeXXbtniMMVvZSHpS/Lz DmoZ2UiWukn3J3/YX69mkkUfr4W/5W16IohtS0MaJJB9PlAYWCwIiMP3B0jnRF8TjD 0kZsNQT6JOamk23kC1R1+zfgyUO0K8eAHdF0m8xc= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-6933] libstdc++: Enforce requirements on template argument of std::optional X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: 68982b98d2a7a52cfc5aada7d35d6c493c010712 X-Git-Newrev: 14f50ba054079eccf9ac49997b92793e2a87b13c Message-Id: <20230329233831.5C0493858D3C@sourceware.org> Date: Wed, 29 Mar 2023 23:38:31 +0000 (GMT) List-Id: https://gcc.gnu.org/g:14f50ba054079eccf9ac49997b92793e2a87b13c commit r13-6933-g14f50ba054079eccf9ac49997b92793e2a87b13c Author: Jonathan Wakely Date: Wed Mar 29 22:02:19 2023 +0100 libstdc++: Enforce requirements on template argument of std::optional The standard does not allow std::optional, std::optional, std::optional etc. and although we do give errors, they come from down inside the internals of std::optional. We could improve the static assertions at the top of the class so that users get a more precise diagnostic: optional:721:21: error: static assertion failed 721 | static_assert(is_object_v<_Tp> && !is_array_v<_Tp>); libstdc++-v3/ChangeLog: * include/std/optional (optional): Adjust static assertion to reject arrays and functions as well as references. * testsuite/20_util/optional/requirements_neg.cc: New test. Diff: --- libstdc++-v3/include/std/optional | 2 +- .../testsuite/20_util/optional/requirements_neg.cc | 24 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/libstdc++-v3/include/std/optional b/libstdc++-v3/include/std/optional index 62ff87a3387..90bf74143f4 100644 --- a/libstdc++-v3/include/std/optional +++ b/libstdc++-v3/include/std/optional @@ -718,7 +718,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { static_assert(!is_same_v, nullopt_t>); static_assert(!is_same_v, in_place_t>); - static_assert(!is_reference_v<_Tp>); + static_assert(is_object_v<_Tp> && !is_array_v<_Tp>); private: using _Base = _Optional_base<_Tp>; diff --git a/libstdc++-v3/testsuite/20_util/optional/requirements_neg.cc b/libstdc++-v3/testsuite/20_util/optional/requirements_neg.cc new file mode 100644 index 00000000000..688c305803e --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/requirements_neg.cc @@ -0,0 +1,24 @@ +// { dg-do compile { target c++17 } } + +#include + +// T shall be a type other than cv in_place_t or cv nullopt_t +// that meets the Cpp17Destructible requirements + +std::optional o1; // { dg-error "here" } +std::optional o2; // { dg-error "here" } +std::optional o3; // { dg-error "here" } +std::optional o4; // { dg-error "here" } +std::optional o5; // { dg-error "here" } +std::optional o6; // { dg-error "here" } +std::optional o7; // { dg-error "here" } +std::optional o8; // { dg-error "here" } + +// { dg-error "static assertion failed" "" { target *-*-* } 0 } + +// { dg-prune-output "forming pointer to reference type" } +// { dg-prune-output "union may not have reference type" } +// { dg-prune-output "function returning an array" } +// { dg-prune-output "flexible array member .* in union" } +// { dg-prune-output "function returning a function" } +// { dg-prune-output "invalidly declared function type" }