C++ Logo

std-discussion

Advanced search

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

From: Lénárd Szolnoki <cpp_at_[hidden]>
Date: Fri, 6 Mar 2026 21:50:53 +0000
On 06/03/2026 17:32, Jens Maurer via Std-Discussion wrote:
>
> Annotations on block-scope function declarations will be disallowed by CWG3124:
>
> https://cplusplus.github.io/CWG/issues/3124.html
>
> Jens

Are extern "C" function declarations in different namespaces in a similar place regarding
this? There is implementation divergence there:

#include <meta>

extern "C" {
[[=1]] void function();

static_assert(annotations_of(^^function).size() == 1);

namespace N {
     [[=2]] void function();
}
}

int main() {
     static constexpr auto NumAnnotations = annotations_of(^^function).size();

     // GCC: 1, clang: 2
     std::printf("Annotation count: %zu", NumAnnotations);
}

https://godbolt.org/z/4qeza4fPv

>
> 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
>
> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion

Received on 2026-03-06 21:51:01