October 21, 2013 Re: Cannot alias expression | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | On Monday, 21 October 2013 at 19:33:07 UTC, simendsjo wrote:
> I've used quite some T(A...), so I was used to literals not being
> aliasable.
> A bit strange that literals "is symbols" (or how I should phrase
> it) when using T(alias A), but not T(A...).
Literals are never symbols. It is a special case for template alias parameter. But alias parameter itself it a symbol and thus can be used with normal alias. Variadic template argument list infers literal as a value, not as a template alias parameter. Values can't be used with normal aliases and thus it fails.
|
October 21, 2013 Re: Cannot alias expression | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | On Monday, 21 October 2013 at 19:14:25 UTC, simendsjo wrote:
> On Monday, 21 October 2013 at 18:42:27 UTC, John Colvin wrote:
>> On Monday, 21 October 2013 at 12:58:55 UTC, John Colvin wrote:
>>> I suspect I'm being very dumb here, but I can't get my head around this:
>>>
>>> template B(alias A)
>>> {
>>> alias B = A;
>>> }
>>> template C(A ...)
>>> {
>>> alias C = A[0];
>>> }
>>> static assert(B!1 == 1); //fine
>>> static assert(C!1 == 1); //Error: cannot alias an expression 1
>>
>> Also:
>>
>> struct S{}
>>
>> template B(alias A)
>> {
>> alias B = A;
>> }
>> template C(A ...)
>> {
>> alias C = A[0];
>> }
>> pragma(msg, B!int); //Error: template instance B!(int)
>> //does not match template declaration B(alias A)
>
> Alias doesn't take primitive types.
Why on earth not?
|
October 21, 2013 Re: Cannot alias expression | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | On Monday, 21 October 2013 at 20:20:45 UTC, John Colvin wrote:
> Why on earth not?
By spec / definition. Alias is intended to only work on symbols (that is the very definition of alias). Unfortunately, template alias parameter has also extra special cases for now good reason which adds confusion.
|
October 22, 2013 Re: Cannot alias expression | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | On 10/21/2013 02:58 PM, John Colvin wrote:
> I suspect I'm being very dumb here, but I can't get my head around this:
>
> template B(alias A)
> {
> alias B = A;
> }
> template C(A ...)
> {
> alias C = A[0];
> }
> static assert(B!1 == 1); //fine
> static assert(C!1 == 1); //Error: cannot alias an expression 1
It is awkward design. I think it should just work. In case you needed to this for anything, the following works:
template ID(alias a){ alias ID = a; } // "identity function on symbols"
template B(alias A){
alias B = A;
}
template C(A ...){
alias C = ID!(A[0]);
}
static assert(B!1 == 1); // fine
static assert(C!1 == 1); // fine
|
Copyright © 1999-2021 by the D Language Foundation