C++ Logo

std-proposals

Advanced search

Re: Allow use of constants and types from not-instantiation-ed template

From: Maciej Polanski <maciejpolanski75_at_[hidden]>
Date: Thu, 30 Dec 2021 22:11:20 +0100
On 2021-12-20 19:08, Hani Deek via Std-Proposals wrote:

Hani,

> I think the proposal should be to allow a namespace to have the same
> name as a class template.
This was my first thought after it was pointed that keeping constants
inside template may generate ambiguity in case of partial specialization
(it allows redefinition of constants).
But I think it can be dangerous to use namespaces, as template name can
accidentally overlap name of the namespace defined elsewhere, generating
errors that are difficult to understand.

It was also pointed later that the traditional way of storing constants
is a non-template public base class, with ios_base given as an example.

> In order for this proposal to work, we will need to add new rules to
> the name lookup rules. We will need to define contexts in which a name
> is assumed to be a namespace name rather than a template name, and
> other contexts for the opposite assumption. We will also need a
> disambiguation mechanism for contexts where both a namespace name and
> a template name may be used.

Well, although most likely I did not fully understood your statement,
wouldn't it make sense to:
"Add to lookup constants and types from non-template public base class
of non-instantiated template (if qualified with template name)"

   struct foo_base{
       constexpr static int bar = 0x01;
   };

   template<int I>
   struct foo : public foo_base{
   };

   foo<foo::bar> foobar; // Basic form
   foo<bar> foobar2; // I would like it, but this may go too far


Thanks,
Maciej

Received on 2021-12-30 15:11:25