1

In my opinion,the using of a function-like macro in C++ is similar to the using of a common function. It seems to be like this:

macroFunctionName(arg1, arg2, arg3);

However, the using of Q_PROPERTY usually looks like this:

Q_PROPERTY(Qt::WindowModality windowModality READ windowModality WRITE setWindowModality)

As we can see, they are different.There is no comma in the using of Q_PROPERTY.I have never seen a function-like macro which was used like Q_PROPERTY.I am even not sure whether Q_PROPERTY is a function-like macro in C++.So is it ill-formed in C++? Or it's just a special syntax for MOC in Qt?

I tried to find it in the C++ standard document but nothing about it was found.

YZ_SO
  • 125
  • 5
  • Related: [What is the significance of Q_PROPERTY in Qt?](https://stackoverflow.com/q/21965777/430766) – bitmask Nov 24 '22 at 06:00

1 Answers1

2

I looked in Qt's ./src/corelib/kernel/qobjectdefs.h file for the definition, and it looks like this:

#define Q_PROPERTY(...) QT_ANNOTATE_CLASS(qt_property, __VA_ARGS__)

... which would make Q_PROPERTY a variadic macro. Of course all it does is expand out to QT_ANNOTATE_CLASS, which is a different macro, one that Qt's moc utility presumably knows how to handle in a meaningful way when generating its moc_*.cpp files.

As for the use of spaces rather than commas; you're right, the preprocessor doesn't treat spaces as argument-separators. I suspect that the C++ preprocessor is simply passing the entire line (i.e. "Qt::WindowModality windowModality READ windowModality WRITE setWindowModality") into the QT_ANNOTATE_CLASS macro as a single argument, and that moc's QT_ANNOTATE_CLASS macro-definition is doing some stringification preprocessor tricks in order to parse it as a string-argument.

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • I suspect `READ`, `WRITE`, and all the other tags you can put in that line, are macros themselves, possibly containing commas themselves. – bitmask Nov 24 '22 at 06:05
  • Hmm, I hadn't thought of that possibility... that might be what they are doing, although a recursive grep of the Qt headers doesn't immediately turn up anything like `#define READ do_something_clever,` – Jeremy Friesner Nov 24 '22 at 06:08
  • `READ` is not a macro. `QT_ANNOTATE_CLASS` expands to nothing, so the compiler ends up seeing nothing. The purpose of `Q_PROPERTY` is for `moc` to parse it. – David Faure Aug 16 '23 at 12:01