woboq / verdigris

Qt without moc: set of macros to use Qt without needing moc
https://woboq.com/blog/verdigris-qt-without-moc.html
GNU Lesser General Public License v3.0
638 stars 58 forks source link

Unused `W_UnscopedName` warnings when defining W_OBJECT in anonymous namespaces #85

Open nyanpasu64 opened 3 years ago

nyanpasu64 commented 3 years ago

Same issue as https://github.com/woboq/verdigris/pull/48. When I define a W_OBJECT in an anonymous namespace, building using Clang (MSVC ABI) on Windows, I get the following kind of warning:

C:/Users/nyanpasu64/code/exotracker-cpp/src/gui/instrument_list.cpp:31:5: warning: unused variable 'W_UnscopedName' [-Wunused-const-variable]
    W_OBJECT(InstrumentListModel)
    ^
C:/Users/nyanpasu64/code/exotracker-cpp/3rdparty\verdigris/wobjectdefs.h:771:5: note: expanded from macro 'W_OBJECT'
    W_OBJECT_COMMON(TYPE) \
    ^
C:/Users/nyanpasu64/code/exotracker-cpp/3rdparty\verdigris/wobjectdefs.h:752:31: note: expanded from macro 'W_OBJECT_COMMON'
        static constexpr auto W_UnscopedName = w_internal::viewLiteral(#TYPE); /* so we don't repeat it in W_CONSTRUCTOR */ \
                              ^

https://github.com/woboq/verdigris/blob/fda6e91391c2e3e58abd4fd996803259fc54905f/src/wobjectdefs.h#L752

This macro turns into a static member variable, declared in a QObject subclass's body, so you can't use Q_UNUSED (which is only valid in functions).

Solutions

[[maybe_unused]] static constexpr auto W_UnscopedName works in my testing, but unfortunately this is C++17-only and Verdigris supports C++14 as well.

Q_DECL_UNUSED static constexpr auto W_UnscopedName also works, but Q_DECL_UNUSED seems to be broken in some Clang configurations: https://bugreports.qt.io/browse/QTBUG-69424

ogoffart commented 3 years ago

Q_DECL_UNUSED should work as it is used within Qt itself.