www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - What is parameter tuple actually?

reply Yang Bo <pop.atry gmail.com> writes:
on gdc, this code does not works:

template TestLines(alias Line) {
  const char[] lineString = Line.stringof;
  const uint line = Line;
}

unittest{
  assert(TestLines!(__LINE__).lineString == __LINE__.stringof);
  assert(TestLines!(__LINE__).line == __LINE__);
}


But, if I use tuple instead, it works:


template TestLines(Args...) {
  const char[] lineString = Args[0].stringof;
  const uint line = Args[0];
}

unittest{
  assert(TestLines!(__LINE__).lineString == __LINE__.stringof);
  assert(TestLines!(__LINE__).line == __LINE__);
}

I wonder why.
Oct 16 2007
parent Daniel Keep <daniel.keep.lists gmail.com> writes:
Yang Bo wrote:
 on gdc, this code does not works:

Please do not say "does not work." That's incredible vague, and doesn't give us any hints as to *why* something doesn't work. If you can, post the output of the compiler.
 template TestLines(alias Line) {
   const char[] lineString = Line.stringof;
   const uint line = Line;
 }
 
 unittest{
   assert(TestLines!(__LINE__).lineString == __LINE__.stringof);
   assert(TestLines!(__LINE__).line == __LINE__);
 }
 
 
 But, if I use tuple instead, it works:
 
 
 template TestLines(Args...) {
   const char[] lineString = Args[0].stringof;
   const uint line = Args[0];
 }
 
 unittest{
   assert(TestLines!(__LINE__).lineString == __LINE__.stringof);
   assert(TestLines!(__LINE__).line == __LINE__);
 }
 
 I wonder why.

I suspect it's because __LINE__ isn't a symbol. __LINE__ is basically replaced by the compiler with its value when it's read from the file (at some stage), and I'll bet it's *before* templates are expanded. So in the first instance, you've got: TestLines!(11).lineString Which won't work because you can't alias an expression. The second one works because it's probably passing the value of __LINE__ as a constant expression, not a symbol alias. -- Daniel
Oct 16 2007