www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - passing non-dynamic arrays to variadic functions

reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
Hello.

let's assume we have this code:

  void doWrite(A...) (A args) {
    import std.stdio;
    import std.conv;
    writeln(to!string(args[0]));
  }

  void main () {
    char[3] a0 =3D "abc";
    char[3] a1 =3D ['a', 'b', 'c'];
    doWrite(a0);
    doWrite(a1);
  }

i don't know why, but this code prints complete garbage each time i run
it. yet if i'll change `doWrite()` invocations to this:

    doWrite(a0[]);
    doWrite(a1[]);

everything is working fine.

am i doing something wrong in the first sample and missed the relevat
part of documentation, or this is a bug? and do we have workaround for
this?
Oct 26 2014
next sibling parent reply "anonymous" <anonymous example.com> writes:
On Monday, 27 October 2014 at 00:26:00 UTC, ketmar via
Digitalmars-d-learn wrote:
 Hello.

 let's assume we have this code:

   void doWrite(A...) (A args) {
     import std.stdio;
     import std.conv;
     writeln(to!string(args[0]));
   }

   void main () {
     char[3] a0 = "abc";
     char[3] a1 = ['a', 'b', 'c'];
     doWrite(a0);
     doWrite(a1);
   }

 i don't know why, but this code prints complete garbage each 
 time i run
Prints abc abc for me.
Oct 26 2014
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 10/26/2014 05:33 PM, anonymous wrote:
 On Monday, 27 October 2014 at 00:26:00 UTC, ketmar via
 Digitalmars-d-learn wrote:
 Hello.

 let's assume we have this code:

   void doWrite(A...) (A args) {
     import std.stdio;
     import std.conv;
     writeln(to!string(args[0]));
   }

   void main () {
     char[3] a0 = "abc";
     char[3] a1 = ['a', 'b', 'c'];
     doWrite(a0);
     doWrite(a1);
   }

 i don't know why, but this code prints complete garbage each time i run
Prints abc abc for me.
Yeah, works with git head. Ali
Oct 26 2014
parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sun, 26 Oct 2014 17:41:28 -0700
Ali =C3=87ehreli via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:

 Yeah, works with git head.
ah, thank you. seems that one of the custom patches i'm using broke that. i just checked this on unpatched DMD, and it works too. sorry for the noise.
Oct 26 2014
prev sibling parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Mon, 27 Oct 2014 00:33:13 +0000
anonymous via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:

 On Monday, 27 October 2014 at 00:26:00 UTC, ketmar via
 Digitalmars-d-learn wrote:
 Hello.

 let's assume we have this code:

   void doWrite(A...) (A args) {
     import std.stdio;
     import std.conv;
     writeln(to!string(args[0]));
   }

   void main () {
     char[3] a0 =3D "abc";
     char[3] a1 =3D ['a', 'b', 'c'];
     doWrite(a0);
     doWrite(a1);
   }

 i don't know why, but this code prints complete garbage each=20
 time i run
=20 Prints abc abc for me.
ah, i forgot to specify my system: GNU/Linux, x86, DMD git head. could you please test this with current git? it seems to work with GDC, which is 2.065 for now. seems that this is a regression.
Oct 26 2014
prev sibling parent reply "MachineCode" <netorib94 gmail.com> writes:
On Monday, 27 October 2014 at 00:26:00 UTC, ketmar via 
Digitalmars-d-learn wrote:
 Hello.

 let's assume we have this code:

   void doWrite(A...) (A args) {
     import std.stdio;
     import std.conv;
     writeln(to!string(args[0]));
   }

   void main () {
     char[3] a0 = "abc";
     char[3] a1 = ['a', 'b', 'c'];
     doWrite(a0);
     doWrite(a1);
   }

 i don't know why, but this code prints complete garbage each 
 time i
run
 it. yet if i'll change `doWrite()` invocations to this:

     doWrite(a0[]);
     doWrite(a1[]);

 everything is working fine.

 am i doing something wrong in the first sample and missed the 
 relevat
 part of documentation, or this is a bug? and do we have 
 workaround for
 this?
It worked fine for me. Output:
 abc
 abc
Environment: Win 8.1 64-bit (but dmd target is 32-bit, IIRC), dmd v2.066.0
Oct 26 2014
parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Mon, 27 Oct 2014 02:19:49 +0000
MachineCode via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:

 It worked fine for me. Output:
=20
 abc
 abc
=20 Environment: Win 8.1 64-bit (but dmd target is 32-bit, IIRC), dmd=20 v2.066.0
yes, thank you too. that was one of my custom patches that broke this. i already found that patch, and it's not even mine! ;-)
Oct 26 2014
parent reply "MachineCode" <netorib94 gmail.com> writes:
On Monday, 27 October 2014 at 02:27:32 UTC, ketmar via 
Digitalmars-d-learn wrote:
 On Mon, 27 Oct 2014 02:19:49 +0000
 MachineCode via Digitalmars-d-learn 
 <digitalmars-d-learn puremagic.com>
 wrote:

 It worked fine for me. Output:
 
 abc
 abc
Environment: Win 8.1 64-bit (but dmd target is 32-bit, IIRC), dmd v2.066.0
yes, thank you too. that was one of my custom patches that broke this. i already found that patch, and it's not even mine! ;-)
You're welcome :) what do you call git head is the dmd compiler compiled from dmd's source code on github?
Oct 26 2014
parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Mon, 27 Oct 2014 03:07:12 +0000
MachineCode via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:

 You're welcome :) what do you call git head is the dmd compiler=20
 compiled from dmd's source code on github?
yes. i'm updating my compiler on dayly basis, so my "git head" is really "head". ;-) but i also have alot of custom patches applied to it (some not-yet-accepted PRs, my internal patches that will never made into mainline and so on). usually i'm trying to reproduce the bug with "vanilla" DMD, but this time i was so sure that it's in vanilla too that i didn't bother to check. my bad.
Oct 26 2014