C++ Logo

std-proposals

Advanced search

Re: Proposal of Namespace Templates (DRAFT 1)

From: Tom Honermann <tom_at_[hidden]>
Date: Tue, 24 Sep 2019 09:58:57 -0400
I have some sympathy for the problem this proposal purports to address.

I think the proposed distinction for initial namespace templates and
namespace template extensions is not well motivated, introduces
gratuitous deviation from existing template syntax, and introduces the
following new problems:

1) Lookup is confusing. Consider the following:

template<int i>
namespace N {}
int i;
namespace template N {
   void f() {
     i; // Ambiguous, ::i, or the i parameter of N?
   }
}

I think this would be a novel and confusing lookup rule no matter which
way the above question is resolved. Some existing cases that require
such semantic lookup as opposed to lexical lookup are for class members
in out of line class member definitions and ADL. For the former, some
guidelines require prefixing class data members with 'm_' or similar to
remind programmers that they are referencing something defined outside
of the lexical context. For the latter, well, issues with ADL are well
known.

2) Parameters of namespace templates can effectively never be omitted
since names are not re-introduced in extensions.

The ability to omit names for unreferenced parameters within a given
scope has a long history in C and C++ and is useful in some cases to
prevent unintended references or to allow different names to be used in
different contexts. In particular for this proposal, I can imagine that
some parameters of namespace templates would only be needed in some
namespace extensions. The traditional template declaration syntax
allows the following:

template<typename, int>
namespace N {}
template<typename T, int>
namespace N {
   void foo(T);
}
template<typename, int I>
namespace N {
   int bar() { return I; }
   template<typename T> // No possible confusion with an outer 'T'.
   void ft(T) { ... }
}

Unrelated to the above, I think the proposal might benefit from some
mechanism for a member of the namespace template to opt out of template
dependency. This would avoid having to introduce additional,
effectively implementation detail namespaces like in the following
example (though, perhaps this approach wouldn't be so bad either):

namespace non_dependent_things_for_N {
   void baz();
}
template<typename>
namespace N {
   using namespace non_dependent_things_for_N;
}

Tom.

On 9/24/19 2:17 AM, Andrew Tomazos via Std-Proposals wrote:
> Please find attached DRAFT 1 of:
>
> Proposal of Namespace Templates
>
> Feedback appreciated.
> -Andrew.
>
>


Received on 2019-09-24 09:01:09