C++ reflection (P2996) and moc: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
This is a WORK IN PROGRESS page to understand the implications of "Reflection for C++" for the future of Qt and moc.  
This is a WORK IN PROGRESS page to understand the implications of "Reflection for C++" for the future of Qt and moc.  


Some questions that this work aims to answer:
== Why? ==
moc extracts interesting metadata from QObject subclasses / gadget / namespaces. This metadata is then used at runtime to implement many different QMetaObject facilities.
 
C++26 will (likely) ship with compile-time reflection. This means that the work that moc does today (as an external tool, with its custom lexer and parser etc.) could be done by the compiler itself. In the future, this may unlock lots of interesting possibilities (e.g. templated QObjects) and reduce technical debt (no need to maintain a C++ parser).
 
However, we're not sure how to get there just yet. Some questions that this work aims to answer:


* Can we replace moc with a pure C++ solution?
* Can we replace moc with a pure C++ solution?
** If yes, how much source code breakage is to be expected?
** Is there something missing from standard C++ that we need for moc?
** Can porting be automated?
* How many source code (API) breaks are to be expected?
* Is there something missing from standard C++ that we need for moc?
** If there's breakage, can porting tools automate the transition?


== Reflection for Standard C++ ==
== Reflection for Standard C++: references ==
C++26 will (likely) ship with compile-time reflection. The papers that describe features that are relevant for Qt are:
Papers that are interesting for Qt:


* P2996 "Reflection for C++26" https://wg21.link/P2996
* P2996 "Reflection for C++26" https://wg21.link/P2996
Line 15: Line 20:
* P3096 "Function Parameter Reflection in Reflection for C++26" https://wg21.link/P3096
* P3096 "Function Parameter Reflection in Reflection for C++26" https://wg21.link/P3096
* P3491 "define_static_{string,object,array}" https://wg21.link/P3491
* P3491 "define_static_{string,object,array}" https://wg21.link/P3491
== moc ==

Revision as of 18:04, 27 May 2025

This is a WORK IN PROGRESS page to understand the implications of "Reflection for C++" for the future of Qt and moc.

Why?

moc extracts interesting metadata from QObject subclasses / gadget / namespaces. This metadata is then used at runtime to implement many different QMetaObject facilities.

C++26 will (likely) ship with compile-time reflection. This means that the work that moc does today (as an external tool, with its custom lexer and parser etc.) could be done by the compiler itself. In the future, this may unlock lots of interesting possibilities (e.g. templated QObjects) and reduce technical debt (no need to maintain a C++ parser).

However, we're not sure how to get there just yet. Some questions that this work aims to answer:

  • Can we replace moc with a pure C++ solution?
    • Is there something missing from standard C++ that we need for moc?
  • How many source code (API) breaks are to be expected?
    • If there's breakage, can porting tools automate the transition?

Reflection for Standard C++: references

Papers that are interesting for Qt:

moc