C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Attributes introspection

From: Giuseppe D'Angelo <giuseppe.dangelo_at_[hidden]>
Date: Mon, 15 Apr 2024 15:23:50 +0200
On 15/04/2024 14:02, Sebastian Wittmeier via Std-Proposals wrote:
> perhaps a small reference to the effect of attributes is suitable, as
> this proposal slightly changes the potential effects of attributes.
>
> If generic code reacts to whether certain attributes are present, then
> attributes can have a direct effect on what a program does.
>
> It is perhaps acceptable, as it is the program (+libraries) itself
> looking at its own (+libraries') attributes, but it is a slight shift
> from "attributes can just be ignored".
>
> Currently the standards committees can make sure, how attributes are used.

*Standard* attributes are under the ignorability rules, but
vendor-specific or library-specific attributes can do whatever they
want. In Qt we use a preprocessing tool (moc) to generate metadata for a
given class (and generate some supporting code); the metadata that the
user wants to extract is marked by macros that normally expand to
nothing. It wouldn't be too far fetched to want to turn those macros
into C++ attributes, and use (generative) reflection instead.

Something like:

> +--------------------------------------------------------------------------+----------------------------------------------------------------------+
> | Now | After |
> +--------------------------------------------------------------------------+----------------------------------------------------------------------+
> | class MyClass : public QObject | class MyClass : public QObject |
> | { | { |
> | Q_OBJECT | Q_OBJECT |
> | | |
> | Q_PROPERTY(int speed READ getSpeed WRITE setSpeed NOTIFY speedChanged) | [[qt::property(READ getSpeed WRITE setSpeed NOTIFY speedChanged)]] |
> | int m_speed; | QProperty<int> m_speed; |
> | | |
> | enum class E { A, B }; | [[qt::enum]] |
> | Q_ENUM(E); | enum class E { A, B }; |
> | | |
> | public: | public: |
> | MyClass(); | MyClass(); |
> | | |
> | Q_SLOT void doSomething(); | [[qt::slot]] void doSomething(); |
> | | |
> | int getSpeed() const; | int getSpeed() const; |
> | void setSpeed(int); | void setSpeed(int); |
> | | |
> | Q_SIGNAL void speedChanged(); | [[qt::signal]] void speedChanged(); |
> | }; | }; |
> +--------------------------------------------------------------------------+----------------------------------------------------------------------+


My 2 c,
--
Giuseppe D'Angelo

Received on 2024-04-15 13:23:54