Skip to content

Commit

Permalink
fix literal_string under gcc
Browse files Browse the repository at this point in the history
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<C, N>::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<C, N>::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<char, 12>::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: DenisYaroshevskiy

Differential Revision: D62217752

fbshipit-source-id: bf2b8892b37848d3e765f6a9752d7ba7a87d5a93
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Sep 9, 2024
1 parent 815f4e3 commit eb62400
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
2 changes: 1 addition & 1 deletion folly/Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ inline constexpr identity_fn identity{};
/// }
template <typename C, std::size_t N>
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) {
Expand Down
2 changes: 2 additions & 0 deletions folly/test/UtilityTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ TEST_F(UtilityTest, forward_like) {
// folly::forward_like<const int&>(1);
}

static_assert(!std::is_default_constructible_v<folly::literal_string<char, 1>>);

TEST_F(UtilityTest, literal_string) {
constexpr auto s = folly::literal_string{"hello world"};
EXPECT_STREQ("hello world", s.c_str());
Expand Down

0 comments on commit eb62400

Please sign in to comment.