C++ Logo

std-proposals

Advanced search

Re: Proposal of Namespace Templates (DRAFT 1)

From: Tom Honermann <tom_at_[hidden]>
Date: Tue, 24 Sep 2019 10:56:50 -0400
On 9/24/19 10:48 AM, Andrew Tomazos via Std-Proposals wrote:
> Thanks for your feedback Tom. You'll find my questions and comments
> below...
>
> On Tue, Sep 24, 2019 at 11:59 PM Tom Honermann <tom_at_[hidden]
> <mailto:tom_at_[hidden]>> wrote:
>
> 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.
>
>
> I don't see why you're reading this any differently than:
>
> namespace N { int i; }
> int i;
> namespace N {
> void f() {
> i;
> }
> }

Because lookup resolving to a parameter not in the lexical context is
novel. I don't believe we have any cases where that occurs today.

>
> Anyway - let's say, for sake of argument, I agreed with you. What
> change to the proposal are you suggesting? The motivation of having
> the template parameters implicitly declared in namespace template
> extensions is so that you only have to define the library
> configuration once, in one source file.
There are other ways to achieve that. For example, by defining the
configuration in a class that is then used as the (sole) class non-type
template parameter for the namespace.
>
> 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) { ... }
> }
>
>
> As proposed the above is ill-formed as you can only have a single
> initial namespace template, the remainder must be namespace template
> extensions - in order to maintain a single definition of the library
> configuration.
I understand that is what is proposed. What I provided above is
intended to illustrate the flexibility trade off that is lost.
>
> If your point is that if we modified the proposal to have namespace
> template extensions redeclare their template parameters, then this
> would allow finer-grained name omission - then I don't think this
> trumps the point above. ie It's more important that we define the
> library configuration once.
I think the consistency argument with other templates is more important
since the single configuration definition goal can be achieved in other
ways.
>
> 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;
> }
>
>
> By what mechanism (if any) can you opt out members of class templates
> from template dependency?
None today.
>
> What is your motivation for wanting to do that?

To avoid redundant template instantiations for entities that are not
affected by the namespace template parameters.

Tom.

> 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:59:02