From 82aa482327f08e770ff25003413315854a0d0c1b Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Fri, 21 Oct 2022 22:50:45 +0800 Subject: [PATCH] Initialize member variable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit as per N4861, 9.4/7.3 [dcl.init], > A class type T is const-default-constructible if default-initialization of T would invoke a user-provided > constructor of T (not inherited from a base class) or if > each direct non-variant non-static data member M of T has a default member initializer or, if M is of > class type X (or array thereof), X is const-default-constructible, and a minimal reproducer is like: struct C { int i; }; const C c; so in this case, we have to initialize the member variable of `custom_char`, so that the class is const-default-constructible. without this change, clang-15 complains like: fmt/src/test/xchar-test.cc:108:8: note: ‘struct custom_char’ has no user-provided default constructor 108 | struct custom_char { | ^~~~~~~~~~~ fmt/src/test/xchar-test.cc:110:3: note: constructor is not user-provided because it is explicitly defaulted in the class body 110 | custom_char() = default; | ^~~~~~~~~~~ fmt/src/test/xchar-test.cc:109:7: note: and the implicitly-defined constructor does not initialize ‘int custom_char::value’ 109 | int value; | ^~~~~ Signed-off-by: Kefu Chai --- test/xchar-test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/xchar-test.cc b/test/xchar-test.cc index 608af350..34dd1c7b 100644 --- a/test/xchar-test.cc +++ b/test/xchar-test.cc @@ -108,7 +108,7 @@ TEST(xchar_test, compile_time_string) { #if FMT_CPLUSPLUS > 201103L struct custom_char { int value; - custom_char() = default; + constexpr custom_char() : value(0) {} template constexpr custom_char(T val) : value(static_cast(val)) {}