www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 533] New: Cannot use int from tuple as an index

reply d-bugmail puremagic.com writes:
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.


-- 
Nov 16 2006
next sibling parent d-bugmail puremagic.com writes:
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]];
  }


-- 
Nov 26 2006
prev sibling next sibling parent d-bugmail puremagic.com writes:
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.)


-- 
Dec 17 2006
prev sibling next sibling parent d-bugmail puremagic.com writes:
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 );
}


-- 
Jun 27 2008
prev sibling parent d-bugmail puremagic.com writes:
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. :-) --
Jun 28 2008