Strange Error using parameterized opAssign for a struct
Robert Jacques
sandford at jhu.edu
Mon Jul 4 07:29:56 PDT 2011
On Mon, 04 Jul 2011 05:58:45 -0400, d coder <dlang.coder at gmail.com> wrote:
> Greetings All
>
> I have a Struct (see minimized code below) which is failing for a simple
> assignment. I am using DMD 2.053, and am getting an error:
>
>> implicit.d(14): Error: cannot implicitly convert expression (foo1) of
>> type Foo!(4) to Foo!(NN)
>
> Am I doing something wrong, or have I hit a DMD bug? Kindly note that
> this happens only when I parameterize the struct using an Integral
> parameter, and works file when using string as parameter (as with struct
> Bar in the code).
>
> Regards
> - Puneet
Yes and No. DMD has trouble deducing the correct template parameters for
implicit function template instantiating when you make the template
parameter the input to another template. The solution is to match a
general type T and constrain it. (DMD can do these more complex matches
inside an is expression, etc. So you can rewrite your code as:
// File implicit.d
struct Foo (size_t N) {
void opAssign (T:Foo!NN,size_t NN)(T f) {/*do nothing*/}
}
struct Bar (string S) {
void opAssign (string SS)(Bar!(SS) f) {/*do nothing*/}
}
void main() {
Bar!"BAR1" bar1;
Bar!"BAR2" bar2;
bar2 = bar1; // this compiles fine
Foo!4 foo1;
Foo!4 foo2;
foo2 = foo1; // Now compiles
}
Also, if you want to instantiate a Foo!NN, IIRC there's similar bug where
the type becomes Foo!NN and not Foo!4. The solution is to use Foo!(NN+0)
(or T) instead.
More information about the Digitalmars-d
mailing list