View mode: basic / threaded / horizontal-split · Log in · Help
August 10, 2012
Re: Access template parameters at runtime
On Friday, 10 August 2012 at 15:26:51 UTC, David Nadlinger wrote:
> It will auto-expand, just when passing it to a method.

Darn, that should have been: »just like when …«

David
August 10, 2012
Re: Access template parameters at runtime
"Henning Pohl" , dans le message (digitalmars.D:174572), a écrit :
> That is what I was trying first, but I could not make it work. 
> Maybe you can show me how it's done?

For example:

import std.stdio;

template TupleToArray(T...)
{
 static if (T.length == 1)
 {
   enum TupleToArray = [T[0]];
 }
 else
 {
   enum TupleToArray = TupleToArray!(T[0..$-1]) ~ T[$-1];
 }
}

void main()
{
 alias TupleToArray!(1, 2, 3) oneTwoThree;
 foreach (i; 0..3)
   writeln(oneTwoThree[i]);
}

output:
1
2
3

TupleToArray should have proper check to make clean code.
There must be something like that somewhere in phobos, or it should be 
added.

-- 
Christophe
August 10, 2012
Re: Access template parameters at runtime
Great, thank you :]

The solution provided by David seems to be shortest. You can even 
pass the ints directly.
August 10, 2012
Re: Access template parameters at runtime
10.08.2012 19:26, David Nadlinger пишет:
> Just use the compiler tuple inside an array literal, like this:
> [integers]. It will auto-expand, just when passing it to a method.

And if it's impossible/undesirable to create an array, you can do this:
---
foreach(i, t; your_tuple)
    if(i == idx)
        return t; // or do something else
assert(0);
---

-- 
Денис В. Шеломовский
Denis V. Shelomovskij
August 10, 2012
Re: Access template parameters at runtime
On 8/10/12, jerro <a@a.com> wrote:
> This would be one way to do it:


On 8/10/12, Christophe Travert <travert@phare.normalesup.org> wrote:
> For example:

Guys I think you're overcomplecating it, you can just do:

struct SomeStruct(integers...)
{
   enum ints = [integers];
   int opIndex(size_t idx) /* ... */
   {
       return ints[idx];
   }
}

enum or static both work.
August 10, 2012
Re: Access template parameters at runtime
On 11-Aug-12 00:58, Andrej Mitrovic wrote:
> On 8/10/12, jerro <a@a.com> wrote:
>> This would be one way to do it:
>
>
> On 8/10/12, Christophe Travert <travert@phare.normalesup.org> wrote:
>> For example:
>
> Guys I think you're overcomplecating it, you can just do:
>
> struct SomeStruct(integers...)
> {
>      enum ints = [integers];
>      int opIndex(size_t idx) /* ... */
>      {
>          return ints[idx];
>      }
> }
>
> enum or static both work.

Internally identical to:

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

Allocates on every call or not? I've no idea maybe Kenji fixed this already.


-- 
Dmitry Olshansky
August 10, 2012
Re: Access template parameters at runtime
On 8/10/12, Dmitry Olshansky <dmitry.olsh@gmail.com> wrote:
> Internally identical to:
>
> int opIndex(size_t idx)
> {
> 	return [integers][idx];
> }
>
> Allocates on every call or not? I've no idea maybe Kenji fixed this
> already.

t's easy to check, add "writeln(&ints[idx]);" in opIndex and index [0]
several times. If the addresses change it means it allocates. It does
allocate every time when it's an enum but not if it's static.
August 10, 2012
Re: Access template parameters at runtime
On 08/10/2012 11:09 PM, Dmitry Olshansky wrote:
> On 11-Aug-12 00:58, Andrej Mitrovic wrote:
>> On 8/10/12, jerro <a@a.com> wrote:
>>> This would be one way to do it:
>>
>>
>> On 8/10/12, Christophe Travert <travert@phare.normalesup.org> wrote:
>>> For example:
>>
>> Guys I think you're overcomplecating it, you can just do:
>>
>> struct SomeStruct(integers...)
>> {
>>      enum ints = [integers];
>>      int opIndex(size_t idx) /* ... */
>>      {
>>          return ints[idx];
>>      }
>> }
>>
>> enum or static both work.
>
> Internally identical to:
>
> int opIndex(size_t idx)
> {
>      return [integers][idx];
> }
>
> Allocates on every call or not? I've no idea maybe Kenji fixed this
> already.
>
>
> --
> Dmitry Olshansky

Still allocates in 2.060. static is the keyword of choice.
Next ›   Last »
1 2
Top | Discussion index | About this forum | D home