D equivalent of C++ explicit

Paul Backus snarwin at gmail.com
Thu Aug 19 18:11:20 UTC 2021


On Thursday, 19 August 2021 at 18:04:58 UTC, Tejas wrote:
> On Thursday, 19 August 2021 at 17:43:59 UTC, Paul Backus wrote:
>> On Thursday, 19 August 2021 at 17:38:14 UTC, Tejas wrote:
>>> As the topic says:
>>>
>>> Is there an equivalent to C++'s `explicit` keyword in D?
>>
>> No, because all constructors are explicit in D.
>
> Oh... then I guess I'll have to manually insert the explicit 
> casts where C++ is doing implicit casts when calling 
> constructors(God help me D: )

Worth noting that in the specific case of variable 
initialization, you do not have to write out the constructor call 
directly:

```d
struct S
{
     this(int n) {}
}

void example()
{
     auto a = S(123); // this calls the constructor
     S b = 456; // so does this
}
```


More information about the Digitalmars-d-learn mailing list