C++ Logo

sg7

Advanced search

Re: [SG7] Reason for members of namespace

From: Andrew Sutton <asutton.list_at_[hidden]>
Date: Tue, 3 Nov 2020 08:58:00 -0500
>
> "Of note here is that we are not proposing a function to retrieve
> members of namespaces..."
>
> I believe "Automatic Class Register" for load/storing frameworks, may be a
> good reason for members of namespace ( obviously filtered ):
>
> I need to get all classes derived from "Widget" (called T in this sample)
> in the namespace "MyFramework" and all of it´s inner namespaces, that has
> funcion "toStream(ostream&)" declared.
>

I also have good uses cases for getting namespace members. The reason we
avoided doing it in P1240 is largely due to technical reasons. For example:

namespace N {
  void f();
  void f();
}

static_assert(members_of(reflexpr(N) == 2); // error or OK?

In Clang, the answer is 2. In GCC, the answer is 1. Which is right? P2237
actually discusses this particular issue: the answer will be 1 because you
have two declarations of the same entity.

Reopening namespaces also poses some interesting issues:

namespane N { void f(); }
static_assert(members_of(reflexpr(N) == 1);
namespane N { void g(); }
static_assert(members_of(reflexpr(N) == 2); // huh

Despite being a constexpr function, members_of returns non-equal values at
different points in the program. Some people tend to be leery about
constexpr functions returning different values depending on where they're
called---they aren't really constant expressions anymore. This would break
an implementation that tries to cache results.

And then there are some sticky questions about how to reflect unnamed
namespaces, using directives, using namespace directives, etc.

Received on 2020-11-03 07:58:14