Arrays of variants, C++ vs D

JN 666total at wp.pl
Thu Jun 17 19:44:31 UTC 2021


This C++ code compiles:
```cpp
#include <variant>
#include <string>
#include <map>

int main()
{
     using Foo = std::variant<int, std::string>;
     std::map<int, Foo> foos =  {{0, "abc"}, {1, 5}};
}

This code doesn't:

```d
import std.variant;

void main()
{
     alias Foo = Algebraic!(int, string);
     Foo[int] foos = [
         0: "abc",
         1: 5
     ];
}
```
but this does:

```d
import std.variant;

void main()
{
     alias Foo = Algebraic!(int, string);
     Foo[int] foos = [
         0: Foo("abc"),
         1: Foo(5)
     ];
}
```

Why does D need the explicit declarations whereas C++ can infer 
it?


More information about the Digitalmars-d-learn mailing list