View mode: basic / threaded / horizontal-split · Log in · Help
August 10, 2012
Access template parameters at runtime
A struct is meant to take only integers as parameters:

struct SomeStruct(intergers...) {
    int opIndex(size_t idx) /* ... */ {
        return integers[idx]; // Error ...
    }
}

alias SomeStruct!(1, 2, 3) ss;


But it results in:
Error: undefined identifier integers, did you mean tuple 
intergers?


How can this problem be solved?
August 10, 2012
Re: Access template parameters at runtime
On 8/10/12 9:55 AM, Henning Pohl wrote:
> A struct is meant to take only integers as parameters:
>
> struct SomeStruct(intergers...) {
> int opIndex(size_t idx) /* ... */ {
> return integers[idx]; // Error ...
> }
> }
>
> alias SomeStruct!(1, 2, 3) ss;
>
>
> But it results in:
> Error: undefined identifier integers, did you mean tuple intergers?
>
>
> How can this problem be solved?

By fixing the typo?

Andrei
August 10, 2012
Re: Access template parameters at runtime
On Friday, 10 August 2012 at 14:02:08 UTC, Andrei Alexandrescu 
wrote:
> On 8/10/12 9:55 AM, Henning Pohl wrote:
>> A struct is meant to take only integers as parameters:
>>
>> struct SomeStruct(intergers...) {
>> int opIndex(size_t idx) /* ... */ {
>> return integers[idx]; // Error ...
>> }
>> }
>>
>> alias SomeStruct!(1, 2, 3) ss;
>>
>>
>> But it results in:
>> Error: undefined identifier integers, did you mean tuple 
>> intergers?
>>
>>
>> How can this problem be solved?
>
> By fixing the typo?
>
> Andrei

Oups, sorry, imagine there isn't one.

So the error is: variable idx cannot be read at compile time.
August 10, 2012
Re: Access template parameters at runtime
On Friday, 10 August 2012 at 14:05:16 UTC, Henning Pohl wrote:
> Oups, sorry, imagine there isn't one.
>
> So the error is: variable idx cannot be read at compile time.

You can't index a tuple during compilation. You need to use an 
array:

struct SomeStruct(alias integers) {
    int opIndex(size_t idx) {
        return integers[idx];
    }
}

alias SomeStruct!([1, 2, 3]) ss;
August 10, 2012
Re: Access template parameters at runtime
On Friday, 10 August 2012 at 14:10:02 UTC, Vladimir Panteleev
wrote:
> On Friday, 10 August 2012 at 14:05:16 UTC, Henning Pohl wrote:
>> Oups, sorry, imagine there isn't one.
>>
>> So the error is: variable idx cannot be read at compile time.
>
> You can't index a tuple during compilation.

Sorry, meant to say - during runtime.
August 10, 2012
Re: Access template parameters at runtime
On Friday, 10 August 2012 at 14:10:38 UTC, Vladimir Panteleev 
wrote:
> On Friday, 10 August 2012 at 14:10:02 UTC, Vladimir Panteleev
> wrote:
>> On Friday, 10 August 2012 at 14:05:16 UTC, Henning Pohl wrote:
>>> Oups, sorry, imagine there isn't one.
>>>
>>> So the error is: variable idx cannot be read at compile time.
>>
>> You can't index a tuple during compilation.
>
> Sorry, meant to say - during runtime.

Thats it, thank you :]
August 10, 2012
Re: Access template parameters at runtime
"Henning Pohl" , dans le message (digitalmars.D:174569), a écrit :
> On Friday, 10 August 2012 at 14:10:38 UTC, Vladimir Panteleev 
> wrote:
>> On Friday, 10 August 2012 at 14:10:02 UTC, Vladimir Panteleev
>> wrote:
>>> On Friday, 10 August 2012 at 14:05:16 UTC, Henning Pohl wrote:
>>>> Oups, sorry, imagine there isn't one.
>>>>
>>>> So the error is: variable idx cannot be read at compile time.
>>>
>>> You can't index a tuple during compilation.
>>
>> Sorry, meant to say - during runtime.
> 
> Thats it, thank you :]

Note that if your design makes that you must have a tuple, you may build 
the array at compile time, so that you can index it at run time.
August 10, 2012
Re: Access template parameters at runtime
On Friday, 10 August 2012 at 14:35:29 UTC, 
travert@phare.normalesup.org (Christophe Travert) wrote:
> "Henning Pohl" , dans le message (digitalmars.D:174569), a 
> écrit :
>> On Friday, 10 August 2012 at 14:10:38 UTC, Vladimir Panteleev 
>> wrote:
>>> On Friday, 10 August 2012 at 14:10:02 UTC, Vladimir Panteleev
>>> wrote:
>>>> On Friday, 10 August 2012 at 14:05:16 UTC, Henning Pohl 
>>>> wrote:
>>>>> Oups, sorry, imagine there isn't one.
>>>>>
>>>>> So the error is: variable idx cannot be read at compile 
>>>>> time.
>>>>
>>>> You can't index a tuple during compilation.
>>>
>>> Sorry, meant to say - during runtime.
>> 
>> Thats it, thank you :]
>
> Note that if your design makes that you must have a tuple, you 
> may build
> the array at compile time, so that you can index it at run time.

That is what I was trying first, but I could not make it work. 
Maybe you can show me how it's done?
August 10, 2012
Re: Access template parameters at runtime
On Friday, 10 August 2012 at 14:42:24 UTC, Henning Pohl wrote:
> That is what I was trying first, but I could not make it work. 
> Maybe you can show me how it's done?

Just use the compiler tuple inside an array literal, like this: 
[integers]. It will auto-expand, just when passing it to a method.

David
August 10, 2012
Re: Access template parameters at runtime
>> Note that if your design makes that you must have a tuple, you 
>> may build
>> the array at compile time, so that you can index it at run 
>> time.
>
> That is what I was trying first, but I could not make it work. 
> Maybe you can show me how it's done?

This would be one way to do it:


auto staticArray(Elements...)(Elements elements)
{
    alias Elements[0] E;
    E[Elements.length] r;

    foreach(i, _; elements)
        r[i] = elements[i];

    return r;
}

struct SomeStruct(integers...) {
    enum arr = staticArray(integers);
    int opIndex(size_t idx){
        return arr[idx];
    }
}
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home