C++ Logo

std-discussion

Advanced search

Re: forward declaration inside another function in a namespace

From: Jens Maurer <jens.maurer_at_[hidden]>
Date: Sun, 30 Jun 2024 12:53:08 +0200
On 30/06/2024 11.45, Federico Kircheis via Std-Discussion wrote:
> On 30/06/2024 01.53, Andrew Schepler wrote:
>> The entities found when searching for a name in a namespace scope are
>> not always the same as the entities which are members of that namespace
>> (and have names). The differences come up with using-declarations,
>> using-directives, friend declarations, extern variables in block scope,
>> and this situation: function declarations in block scope.
>
> Is this difference desirable or necessary?
> Function declarations in block scope do not happen often, but since
> "using-declarations, using-directives, friend declarations, extern
> variables in block scope" are also affected, it seems desirable to me to
> have the same rules in all situations.

The rules have been like this since the dawn of C++. For example:

namespace N {
  struct S {
    friend bool f();
  };

  bool b = ::N::f(); // error: does not find f

  bool f() { return false; }
}

If you feel a change is warranted in this area, this needs
a paper with substantial rationale.

In short, why should a declaration that is in a block scope
ever affect name lookup outside of that block scope?
Note that declaration matching is different from name lookup.

Jens

Received on 2024-06-30 10:53:12