public inbox for libstdc++-cvs@sourceware.org help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-6933] libstdc++: Enforce requirements on template argument of std::optional Date: Wed, 29 Mar 2023 23:38:31 +0000 (GMT) [thread overview] Message-ID: <20230329233831.5C0493858D3C@sourceware.org> (raw) https://gcc.gnu.org/g:14f50ba054079eccf9ac49997b92793e2a87b13c commit r13-6933-g14f50ba054079eccf9ac49997b92793e2a87b13c Author: Jonathan Wakely <jwakely@redhat.com> 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<T&>, std::optional<T[1]>, std::optional<T()> 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<remove_cv_t<_Tp>, nullopt_t>); static_assert(!is_same_v<remove_cv_t<_Tp>, 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 <optional> + +// T shall be a type other than cv in_place_t or cv nullopt_t +// that meets the Cpp17Destructible requirements + +std::optional<std::nullopt_t> o1; // { dg-error "here" } +std::optional<const std::nullopt_t> o2; // { dg-error "here" } +std::optional<std::in_place_t> o3; // { dg-error "here" } +std::optional<const std::in_place_t> o4; // { dg-error "here" } +std::optional<int&> o5; // { dg-error "here" } +std::optional<int[1]> o6; // { dg-error "here" } +std::optional<int[]> o7; // { dg-error "here" } +std::optional<int()> 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" }
reply other threads:[~2023-03-29 23:38 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20230329233831.5C0493858D3C@sourceware.org \ --to=redi@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).