Skip to content

[dcl.attr.annotation] Disallow annotations on "remote" declarations #809

@katzdm

Description

@katzdm

Full name of submitter: Dan Katz

Reference: [dcl.attr.annotation]

Issue description
Other than new and novel forms of friend injection, there is currently very little motivation for allowing annotations on declarations of block-scope function and extern variable declarations, or on friend declarations. Consider:

template <int>
class Counter {
  struct S;
public:
  template <auto = []{}>
  static consteval size_t value() {
    void f(S);

    constexpr size_t result = annotations_of(^^f).size();
    return result;
  }

  template <auto = []{}>
  static consteval size_t incr() {
    [[=1]] void f(S);

    constexpr size_t result = annotations_of(^^f).size();
    return result;
  }
};

static_assert(Counter<0>::value() == 0);
static_assert(Counter<0>::incr() == 1);
static_assert(Counter<0>::incr() == 2);
static_assert(Counter<1>::incr() == 1);
static_assert(Counter<0>::incr() == 3);
static_assert(Counter<0>::value() == 3);
static_assert(Counter<1>::value() == 1);

(note: the above is currently IFNDR, but will be well-formed if the solution proposed for #808 is accepted; these issues are therefore related)

We should just do the conservative thing and disallow such annotations for C++26; ~nobody will miss them.

Suggested resolution
Modify [dcl.attr.annotation]/1 as follows:

An annotation may be applied to any a declaration of a type, type alias, variable, function, namespace, enumerator, base-specifier, or non-static data member. No annotation shall apply to a declaration D, nor to a function parameter declaration in the function-declarator (if any) of D, if either

  • D is a friend declaration ([class.friend]) or
  • the host scope of D ([basic.scope.scope]) is not the target scope of D.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions