From: Jonathan Wakely Date: Wed, 29 Mar 2023 21:02:19 +0000 (+0100) Subject: libstdc++: Enforce requirements on template argument of std::optional X-Git-Tag: basepoints/gcc-14~274 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=14f50ba054079eccf9ac49997b92793e2a87b13c;p=thirdparty%2Fgcc.git 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 --git a/libstdc++-v3/include/std/optional b/libstdc++-v3/include/std/optional index 62ff87a33873..90bf74143f4f 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 000000000000..688c305803e2 --- /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" }