Skip to content

Avoid C++20 deprecated assignment to volatile

stbergmann requested to merge stbergmann/glib:deprecatedvolatile into master

794c1a30 "macro wrappers for g_once_init_enter/leave" added this line (whose intent is unclear to me).

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1152r4.html "Deprecating volatile" (scheduled for inclusion in C++20) will make the assignment expression

*(location) = (result)

deprecated when the LHS is of (non-class) volatile type, which is the case when g_once_init_leave is expanded as part of e.g. G_DEFINE_TYPE_WITH_CODE (in gobject/gtype.h), where location is a pointer to some

static volatile gsize g_define_type_id__volatile = 0;

Recent Clang trunk emits -Wdeprecated-volatile for it under -std=c++2a since <https://github.com/llvm/llvm-project/commit/ 4a6861a7e5b59be24a09b8b9782255d028e7aade> "[c++20] P1152R4: warn on any simple-assignment to a volatile lvalue".

The fix is to make the assignment expression a discared-value expression by casting it to void (which in turn requires casting the second branch of the surrounding conditional expression to void, too; not sure what the top-level cast to void was intended for, and whether it would still be needed under certain circumstances).

Merge request reports