Hi, i have a compile problem with Clang 20.1 ! Older versionof Clang can compile the…
- User
Hi, i have a compile problem with Clang 20.1 ! Older versionof Clang can compile the file 3rdparty\bgfx\3rdparty\dear-imgui\imgui.h. See here https://raw.githubusercontent.com/mamedev/mame/refs/heads/master/3rdparty/bgfx/3rdparty/dear-imgui/imgui.cpp
The compile error is this here:
Clang 20.1.0 detected Compiling 3rdparty/bgfx/3rdparty/dear-imgui/imgui.cpp... In file included from ../../../../../3rdparty/bgfx/3rdparty/dear-imgui/imgui.cpp:856: ../../../../../3rdparty/bgfx/3rdparty/dear-imgui/imgui.h:2451:43: error: first argument in call to 'memset' is a pointer to non-trivially copyable type 'ImDrawListSplitter' [-Werror,-Wnontrivial-memcall] 2451 | inline ImDrawListSplitter() { memset(this, 0, sizeof(this)); } | ^ ../../../../../3rdparty/bgfx/3rdparty/dear-imgui/imgui.h:2451:43: note: explicitly cast the pointer to silence this warning 2451 | inline ImDrawListSplitter() { memset(this, 0, sizeof(this)); } | ^ | (void) ../../../../../3rdparty/bgfx/3rdparty/dear-imgui/imgui.h:2522:60: error: first argument in call to 'memset' is a pointer to non-trivially copyable type 'ImDrawList' [-Werror,-Wnontrivial-memcall] 2522 | ImDrawList(ImDrawListSharedData shared_data) { memset(this, 0, sizeof(this)); _Data = shared_data; } | ^ ../../../../../3rdparty/bgfx/3rdparty/dear-imgui/imgui.h:2522:60: note: explicitly cast the pointer to silence this warning 2522 | ImDrawList(ImDrawListSharedData shared_data) { memset(this, 0, sizeof(this)); _Data = shared_data; } | ^ | (void)
- Assistant
The warning you are encountering in Clang 20.1 is due to the use of
memseton classes or structs that are defined as non-trivially copyable. In C++, a non-trivially copyable class typically has non-POD (Plain Old Data) members, such as non-trivial constructors, destructors, or other user-defined types.Here's how you can address this issue:
-
Avoid Using
memset: Instead of usingmemsetto zero-initialize your objects, you can initialize the members explicitly or use an initializer list. -
Use Default Member Initializers: If the members can be default initialized, you can initialize them directly in the class definition.
-
Use
std::fillor Other Initialization Methods: If your class has members that require specific initialization, do not usememset.
Here is an example of how you might modify the constructors of
ImDrawListSplitterandImDrawListinimgui.h:Original Code
inline ImDrawListSplitter() { memset(this, 0, sizeof(*this)); } ImDrawList(ImDrawListSharedData* shared_data) { memset(this, 0, sizeof(*this)); _Data = shared_data; }Modified Code
You can replace the
memsetcalls with value initialization:inline ImDrawListSplitter() : SomeMember{0}, AnotherMember{0} { } // Initialize each member ImDrawList(ImDrawListSharedData* shared_data) : _Data(shared_data), SomeOtherMember{0} { } // Initialize members directlyGeneral Example
Suppose
ImDrawListSplitterlooks like this:struct ImDrawListSplitter { int Count; // Other members... inline ImDrawListSplitter() : Count(0) { /* initialize other members */ } };Replace all instances of
memset(this, 0, sizeof(*this));with proper member initialization.Conclusion
This change not only resolves the warning with
memset, but it also aligns better with the C++ standards regarding object initialization, ensuring that constructors or destructors are invoked properly.Make sure to apply similar changes throughout your code wherever you are initializing non-trivially copyable types with
memset. -
- User
Thx1
- Assistant
You're welcome! If you have any more questions or need further assistance, feel free to ask. Happy coding!