From 64a23d6ed56dff355a4604061fcb393e44ed26ef Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Tue, 3 Sep 2024 10:57:31 -0700 Subject: [PATCH] fix literal_string under gcc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Fixes this compile error under gcc: ``` folly/test/UtilityTest.cpp: In member function ‘virtual void UtilityTest_literal_string_Test::TestBody()’: folly/test/UtilityTest.cpp:195:57: error: ‘constexpr folly::literal_string::literal_string(const C (&)[N]) [with C = char; long unsigned int N = 12]’ called in a constant expression 195 | constexpr auto s = folly::literal_string{"hello world"}; | ^ In file included from folly/test/UtilityTest.cpp:17: folly/Utility.h:279:34: note: ‘constexpr folly::literal_string::literal_string(const C (&)[N]) [with C = char; long unsigned int N = 12]’ is not usable as a ‘constexpr’ function because: 279 | FOLLY_CONSTEVAL /* implicit */ literal_string(C const (&buf)[N]) noexcept { | ^~~~~~~~~~~~~~ folly/Utility.h:279:34: error: member ‘folly::literal_string::buffer’ must be initialized by mem-initializer in ‘constexpr’ constructor folly/Utility.h:277:5: note: declared here 277 | C buffer[N]; | ^~~~~~ ``` And add the corresponding test to the cmake build. Reviewed By: Gownta Differential Revision: D62076629 fbshipit-source-id: fd0ecf1e69625215f48c662ad7ee07ff6f330f6d --- CMakeLists.txt | 1 + folly/Utility.h | 2 +- folly/test/UtilityTest.cpp | 2 ++ 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6c974642ff9..b729413c20b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1156,6 +1156,7 @@ if (BUILD_TESTS OR BUILD_BENCHMARKS) TEST unit_test SOURCES UnitTest.cpp BENCHMARK uri_benchmark SOURCES UriBenchmark.cpp TEST uri_test SOURCES UriTest.cpp + TEST utility_test SOURCES UtilityTest.cpp TEST varint_test SOURCES VarintTest.cpp DIRECTORY testing/test/ diff --git a/folly/Utility.h b/folly/Utility.h index 2e9d6a6b9f8..8b40205c8af 100644 --- a/folly/Utility.h +++ b/folly/Utility.h @@ -274,7 +274,7 @@ inline constexpr identity_fn identity{}; /// } template struct literal_string { - C buffer[N]; + C buffer[N] = {}; FOLLY_CONSTEVAL /* implicit */ literal_string(C const (&buf)[N]) noexcept { for (std::size_t i = 0; i < N; ++i) { diff --git a/folly/test/UtilityTest.cpp b/folly/test/UtilityTest.cpp index 32867ceade2..430e5776f9b 100644 --- a/folly/test/UtilityTest.cpp +++ b/folly/test/UtilityTest.cpp @@ -191,6 +191,8 @@ TEST_F(UtilityTest, forward_like) { // folly::forward_like(1); } +static_assert(!std::is_default_constructible_v>); + TEST_F(UtilityTest, literal_string) { constexpr auto s = folly::literal_string{"hello world"}; EXPECT_STREQ("hello world", s.c_str());