std.conv.to!string(array), strange compile error

Relja via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Nov 14 04:46:18 PST 2015


I've got this strange compile error using 
std.conv.to!string(double[3]) - or any static array type. It's 
called in toString override function of a template matrix class, 
I'm building as a D learning project.

Here is the toString method:
override string toString() const
{
     string outs;
     outs ~= "[";

     for (size_t i = 0; i < rows; ++i)
     {
         outs ~= this.opIndex(i, 0).to!string;
         for (size_t j = 1; j < cols; ++j)
         {
             outs~= ", " ~ this.opIndex(i, j).to!string;
         }
         if (i == rows - 1) // last row
             outs ~= "]";
         else
             outs ~= ";\n";
     }
     return outs;
}

And here is the opIndex (const version):
const(T) opIndex(ulong r, ulong c) const
in
{
     assert(r < _rows && c < _cols);
}
body
{
     return _begin[c*_strides[1] + r*_strides[0]];
}

Error occurs on lines with "this.opIndex().to!string", when 
template type is static array, e.g. double[3]:

source/matrix.d(248,30): Error: template std.conv.to cannot 
deduce function from argument types !(string)(const(double[3])), 
candidates are:
/usr/include/dlang/dmd/std/conv.d(293,1):        std.conv.to(T)
source/matrix.d(251,37): Error: template std.conv.to cannot 
deduce function from argument types !(string)(const(double[3])), 
candidates are:
/usr/include/dlang/dmd/std/conv.d(293,1):        std.conv.to(T)
source/main.d(17,2): Error: template instance 
matrix.Matrix!(double[3]) error instantiating

Lines 248 and 251 are the opIndex.to!string calls.


Anyhow, I've changed opIndex() calls in the toString with matrix 
array indexing implementation (_begin[row*col_stride + 
col*item_stride]), and the conversion works properly (as it 
should on any array type, and gives normal output). So I've made 
a little test with compile-time messages:

override string toString() const
{
     string outs;
     outs ~= "[";

     pragma(msg, typeof(_begin[0]).stringof);
     pragma(msg, typeof(this.opIndex(0, 0)).stringof);

     this._begin[0].to!string;
     (this.opIndex(0, 0)).to!string;

     .....

And here's the compile output:

const(double[3])
const(double[3])
source/matrix.d(250): Error: template std.conv.to cannot deduce 
function from argument types !(string)(const(double[3])), 
candidates are:
/usr/include/dlang/dmd/std/conv.d(293):        std.conv.to(T)

Line 250 is opIndex().to!string, line above compiles ok.

So even opIndex has the same return type as the array itself, 
to!string produces compile error on the opIndex.to!string call, 
and on the array.to!string does not. Could this be a bug in the 
compiler, or am I doing something wrong with the opIndex (or any 
other) method declaration?

System is x64 Manjaro linux(arch based distro), compiler is dmd 
v2.069 (up-to-date from AUR, as libphobos is), and I have no 
other D compilers installed. Im' not sure how to check exact 
version of the libphobos - if anyone can tell me how to check it, 
please tell me, and I'll post it.

Any other suggestion about above code is welcome!

Thanks,
Relja Ljubobratovic




More information about the Digitalmars-d-learn mailing list