From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2116) id B62A33938C06; Sun, 12 Jul 2020 17:45:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B62A33938C06 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1594575948; bh=qP0UfmvDcWOZmi4MYr+NsxCFNMJmIyx+4nEPc81GbYI=; h=From:To:Subject:Date:From; b=uAt6lED3qoJM8WxLFaHf+yhA1QTzqd+8BnCSei/FdNjoavFDRR3nnkerWiTrGSg4Q DCv29QN/p7+HYwVQ4Pf5YzsP+M2Wh2dsTnU7ccAyDyAxnQAWlwVcOq/qgDC1bL+Bpi A0p1gvTAGhu6hpbaPxgHJE1GR/wMYBJ8q/Qw0Rb4= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Ian Lance Taylor To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc/devel/gccgo] libstdc++: Make byte-sized std::fill_n a constant expression (PR 94933) X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/devel/gccgo X-Git-Oldrev: 18a6e4130fb3c63a974295a18ed6e4bef070df03 X-Git-Newrev: 22b6b5d6cfb76deb68ca5bc0bcae8b4245df946d Message-Id: <20200712174548.B62A33938C06@sourceware.org> Date: Sun, 12 Jul 2020 17:45:48 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 12 Jul 2020 17:45:48 -0000 https://gcc.gnu.org/g:22b6b5d6cfb76deb68ca5bc0bcae8b4245df946d commit 22b6b5d6cfb76deb68ca5bc0bcae8b4245df946d Author: Jonathan Wakely Date: Sun May 3 12:46:15 2020 +0100 libstdc++: Make byte-sized std::fill_n a constant expression (PR 94933) The overload for byte types uses memset and isn't constexpr. This adds the specifier and uses std::is_constant_evaluated() to provide a compile-time alternative. PR libstdc++/94933 * include/bits/stl_algobase.h (__fill_a1): Make overload for byte types usable in constant expressions. * testsuite/25_algorithms/fill_n/constexpr.cc: Test with bytes and non-scalars. Diff: --- libstdc++-v3/ChangeLog | 8 ++++++ libstdc++-v3/include/bits/stl_algobase.h | 9 ++++++ .../testsuite/25_algorithms/fill_n/constexpr.cc | 32 +++++++++++++++++++++- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 1bf10811c3b..7f11117365e 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,11 @@ +2020-05-03 Jonathan Wakely + + PR libstdc++/94933 + * include/bits/stl_algobase.h (__fill_a1): Make overload for byte types + usable in constant expressions. + * testsuite/25_algorithms/fill_n/constexpr.cc: Test with bytes and + non-scalars. + 2020-05-01 Jonathan Wakely PR libstdc++/94901 diff --git a/libstdc++-v3/include/bits/stl_algobase.h b/libstdc++-v3/include/bits/stl_algobase.h index a7e92d4b473..bed61fd9d00 100644 --- a/libstdc++-v3/include/bits/stl_algobase.h +++ b/libstdc++-v3/include/bits/stl_algobase.h @@ -875,11 +875,20 @@ _GLIBCXX_END_NAMESPACE_CONTAINER // Specialization: for char types we can use memset. template + _GLIBCXX20_CONSTEXPR inline typename __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type __fill_a1(_Tp* __first, _Tp* __last, const _Tp& __c) { const _Tp __tmp = __c; +#if __cpp_lib_is_constant_evaluated + if (std::is_constant_evaluated()) + { + for (; __first != __last; ++__first) + *__first = __tmp; + return; + } +#endif if (const size_t __len = __last - __first) __builtin_memset(__first, static_cast(__tmp), __len); } diff --git a/libstdc++-v3/testsuite/25_algorithms/fill_n/constexpr.cc b/libstdc++-v3/testsuite/25_algorithms/fill_n/constexpr.cc index c18b6c99eda..17c9aa5a8d7 100644 --- a/libstdc++-v3/testsuite/25_algorithms/fill_n/constexpr.cc +++ b/libstdc++-v3/testsuite/25_algorithms/fill_n/constexpr.cc @@ -28,7 +28,37 @@ test() const auto outd = std::fill_n(ma0.begin(), 6, 77); - return outd == ma0.begin() + 6; + return outd == ma0.begin() + 6 && ma0[5] == 77 && ma0[6] == 0; } static_assert(test()); + +constexpr bool +test_byte() +{ + // PR libstdc++/94933 + std::array ma0{}; + + const auto outd = std::fill_n(ma0.begin(), 6, 77); + + return outd == ma0.begin() + 6 && ma0[5] == 77 && ma0[6] == 0; +} + +static_assert( test_byte() ); + +struct S +{ + int i = 0; +}; + +constexpr bool +test_nonscalar() +{ + std::array ma0{}; + + const auto outd = std::fill_n(ma0.begin(), 6, S{77}); + + return outd == ma0.begin() + 6 && ma0[5].i == 77 && ma0[6].i == 0; +} + +static_assert( test_nonscalar() );