Arrays of variants, C++ vs D

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Jun 17 20:15:24 UTC 2021


On Thu, Jun 17, 2021 at 07:44:31PM +0000, JN via Digitalmars-d-learn wrote:
[...]
>     Foo[int] foos = [
>         0: Foo("abc"),
>         1: Foo(5)
>     ];
> }
> ```
> 
> Why does D need the explicit declarations whereas C++ can infer it?

Because D does not support implicit construction. The array literal is
parsed as-is, meaning string[int] is inferred rather than Foo[int]. So
the initialization fails because of a type mismatch.

Implicit construction has been asked for many times, but Walter has been
adamant about not allowing implicit construction in D.


T

-- 
Music critic: "That's an imitation fugue!"


More information about the Digitalmars-d-learn mailing list