C++ Logo

std-discussion

Advanced search

Re: Behavior of annotations on block-scope function declarations in templates

From: Keenan Horrigan <friedkeenan_at_[hidden]>
Date: Fri, 06 Mar 2026 18:16:56 +0000
Shucks, ah well.

Thanks for the info.


On Friday, March 6th, 2026 at 11:32 AM, Jens Maurer <jens.maurer_at_[hidden]> wrote:

>
> Annotations on block-scope function declarations will be disallowed by CWG3124:
>
> https://cplusplus.github.io/CWG/issues/3124.html
>
> Jens
>
>
> On 3/6/26 03:06, Keenan Horrigan via Std-Discussion wrote:
> > Hello,
> >
> > I was wondering what the proper behavior of the following code should be:
> >
> > #include <meta>
> >
> > [[=1]]
> > void function();
> >
> > static_assert(annotations_of(^^function).size() == 1);
> >
> > template<auto Value>
> > consteval void annotate_function() {
> > [[=Value]]
> > void function();
> > }
> >
> > consteval {
> > annotate_function<2>();
> > }
> >
> > int main() {
> > static constexpr auto NumAnnotations = annotations_of(^^function).size();
> >
> > std::printf("Annotation count: %zu", NumAnnotations);
> > }
> >
> > Godbolt link: https://godbolt.org/z/4dca5xEPe
> >
> > With GCC trunk, it prints "Annotation count: 1", but with the experimental Clang reflection branch it prints "Annotation count: 3". I assume Clang here is acting erroneously in some regard because it appears to be duplicating the initial annotation placed on the function. But what I'm more interested in is whether the compiler should be appending the annotation from annotate_function globally.
> >
> > As well, if one removes the templating from annotate_function, GCC continues to report only one annotation, but Clang reports two: https://godbolt.org/z/74evKWT3a
> >
> > I'd also be interested in how that is meant to behave.
> >
> > Thanks
>
>

Received on 2026-03-06 18:17:06