C++ Logo

std-proposals

Advanced search

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

From: William Linkmeyer <wlink10_at_[hidden]>
Date: Mon, 20 Dec 2021 14:24:57 -0500
Let me take back that last message — this in hindsight looks like more work than I gave it thought for. Specifically, how a templating block would be able to deduce in which templates to apply the given class and which not to deserves much more though than I gave it.

WL

> On Dec 20, 2021, at 2:18 PM, William Linkmeyer <wlink10_at_[hidden]> wrote:
>
> Maybe introducing friendlier syntax for instantiating templates would work. Something like a “templating” block, similar to how the “using” block works with namespaces.
>
> i.e.:
>
> templating<class SomeClass> {
> auto variable = Foo::bar;
> }
>
> Which would resolve as
>
> auto variable = Foo<SomeClass>::bar;
>
> WL
>
>>> On Dec 20, 2021, at 1:09 PM, Hani Deek via Std-Proposals <std-proposals_at_[hidden]> wrote:
>>>
>> 
>> >
>> > 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.
>> --
>> Std-Proposals mailing list
>> Std-Proposals_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2021-12-20 13:24:59