Thread overview
[Issue 533] New: Cannot use int from tuple as an index
Nov 16, 2006
d-bugmail
Nov 27, 2006
d-bugmail
Dec 17, 2006
d-bugmail
Jun 28, 2008
d-bugmail
Jun 28, 2008
d-bugmail
November 16, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=533

           Summary: Cannot use int from tuple as an index
           Product: D
           Version: 0.174
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: wbaxter@gmail.com


An int in a tuple cannot be used as an index for a tuple, even if it is completely determined at compile time.

---------------------------
// This doesn't work
template Nth(TList...) {
    const int N = TList[0];
    auto Nth = TList[N];
}
void main()
{
    writefln( Nth!(1,"hi","there") );
}
-------------

In this case the sample can be fixed by using:

   template Nth(int N, TList...) {

But that's not always the case.


-- 

November 27, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=533





------- Comment #1 from wbaxter@gmail.com  2006-11-26 19:43 -------
As of DMD 0.175 this can also be worked around by eliminating the N, and indexing directly with TList[0]:

  template Nth(TList...) {
      auto Nth = TList[TList[0]];
  }


-- 

December 17, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=533





------- Comment #2 from wbaxter@gmail.com  2006-12-17 02:37 -------
Just retested with 0.177.  This behavior is still there.
(I thought there was a chance it might have been fixed by the last two rounds
of bug slaughters.)


-- 

June 28, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=533


bugzilla@digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |WORKSFORME




------- Comment #3 from bugzilla@digitalmars.com  2008-06-28 01:36 -------
The example is broken because in order for the 'promotion' of the member Nth, it must be the only member. The following works:

import std.stdio;

template Nth(TList...) {
    const int N = TList[0];
    auto Nth = TList[N];
}
void main()
{
    writefln( Nth!(1,"hi","there").Nth );
}


-- 

June 28, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=533





------- Comment #4 from wbaxter@gmail.com  2008-06-28 02:48 -------
(In reply to comment #3)
> The example is broken because in order for the 'promotion' of the member Nth, it must be the only member. The following works:
> 
> import std.stdio;
> 
> template Nth(TList...) {
>     const int N = TList[0];
>     auto Nth = TList[N];
> }
> void main()
> {
>     writefln( Nth!(1,"hi","there").Nth );
> }
> 

Good point.  Or use the version I posted above without the intermediate variable.  I blame the misunderstanding on my youth at the time of reporting this one.  :-)


--