diff --git a/glib/gmacros.h b/glib/gmacros.h index d5b2f30c1..c686e8094 100644 --- a/glib/gmacros.h +++ b/glib/gmacros.h @@ -1239,11 +1239,9 @@ #if G_GNUC_CHECK_VERSION(2, 0) && defined(__OPTIMIZE__) #define _G_BOOLEAN_EXPR_IMPL(uniq, expr) \ G_GNUC_EXTENSION ({ \ - int G_PASTE (_g_boolean_var_, uniq); \ + int G_PASTE (_g_boolean_var_, uniq) = 0; \ if (expr) \ G_PASTE (_g_boolean_var_, uniq) = 1; \ - else \ - G_PASTE (_g_boolean_var_, uniq) = 0; \ G_PASTE (_g_boolean_var_, uniq); \ }) #define _G_BOOLEAN_EXPR(expr) _G_BOOLEAN_EXPR_IMPL (__COUNTER__, expr) diff --git a/glib/tests/cxx.cpp b/glib/tests/cxx.cpp index 4b4dd5800..e00ffd407 100644 --- a/glib/tests/cxx.cpp +++ b/glib/tests/cxx.cpp @@ -536,6 +536,25 @@ test_string_free (void) g_free (data); } +#if G_CXX_STD_CHECK_VERSION(14) +static constexpr gboolean +g_likely_test_expr (void) +{ + if G_LIKELY (1 == 1) + { + return TRUE; + } +} + +static void +test_constexpr_var_init (void) +{ + g_test_message ("Test that G_LIKELY macro creates an initialized variable " + "for compatibility with constexpr"); + g_assert_true (g_likely_test_expr ()); +} +#endif + #ifdef G_OS_WIN32 static void test_clear_com (void) @@ -595,6 +614,10 @@ main (int argc, char *argv[]) g_test_add_func ("/C++/string-append", test_string_append); g_test_add_func ("/C++/string-free", test_string_free); +#if G_CXX_STD_CHECK_VERSION(14) + g_test_add_func ("/C++/constexpr-var-init", test_constexpr_var_init); +#endif + #ifdef G_OS_WIN32 g_test_add_func ("/C++/test_clear_com", test_clear_com); #endif