C++ Logo


Advanced search

Re: Request for supporting and enumerating user-defined attributes

From: Peter Dimov <pdimov_at_[hidden]>
Date: Mon, 16 Oct 2023 12:15:27 +0300
Jeremy Ong wrote:
> I suspect that reflection on the attributes we have today are useful (e.g.
> querying if a function is marked [[deprecated]] or not), so perhaps it would be
> easier for compiler authors to simply retain reflection information as opposed
> to creating an orthogonal syntax (and adding reflection support for that also).

It will be useful, and we will most likely get that eventually, but it's not what
we want for user-defined annotations.

Expressions are much more flexible than attributes. As an example, let's take
the closest we have today to an attribute-like annotation, alignas. You can
do things with it like

#define MY_ALIGNMENT 4
constexpr int my_alignment = 4;
constexpr int calc_align( int x ) { return x*x; }

alignas(MY_ALIGNMENT) int x1;
alignas(my_alignment) int x2;
alignas(calc_align(2)) int x3;
alignas([](int x){ return x*x;}(2)) int x4;

In contrast, an (unknown to the compiler) attribute will at most give you the
text. I'm not even sure that the MY_ALIGNMENT macro will expand.

Received on 2023-10-16 09:15:30