C++ Logo

std-proposals

Advanced search

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

From: Hani Deek <hani_deek_at_[hidden]>
Date: Mon, 20 Dec 2021 18:08:42 +0000
>
> Hello,
>
> So, let's just allow use of a types and static constants from
> not-instantiation-ed template class (if possible):
>
> template<typename T>
> struct Foo{
> constexpr static int bar = 0x01;
> };
> int i = Foo::bar; // Doesn't compile now, but could
> int j = Foo<class Nonexisting>::bar; // Ugly hack but works
>

I think the proposal should be to allow a namespace to have the same name as a class template.

For example,

---------------------------------
namespace Foo
{
  constexpr int bar = 0x01;
}

template<typename T>
struct Foo
{
   static constexpr T bar = 0x02;
};

int i = Foo::bar; //Foo is a namespace name
int j = Foo<short>::bar; //Foo is a class template name
---------------------------------

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.

Adding those new name lookup rules will be a radical change to C++, and I think that many people will oppose the proposal on the grounds that the benefits are not worth the hassle.

Received on 2021-12-20 12:08:52