What is the difference between these template declaration forms?

evilrat evilrat666 at gmail.com
Sat May 15 09:43:21 UTC 2021


On Saturday, 15 May 2021 at 08:37:21 UTC, cc wrote:
> Are these identical?  Or is there a different usage for the (T 
> : something) form?
> ```d
> 	auto opCast(T)() if (is(T == bool)) {
> 		return _obj !is null;
> 	}
> ```

They are the same in this case.

> ```d
> 	auto opCast(T : bool)() {
> 		return _obj !is null;
> 	}
> ```

While the docs says it matches the most specialized type, which 
is bool in this case, IIRC it is actually takes anything 
convertible to bool despite what the docs says.
You should double check it yourself.

Anyway I would usually pick this form where possible as it yields 
cleaner error messages unlike arbitrary template constraint 
"stuff X doesnt takes Y, did you mean (basically X but not)" 
message that who knows what it wants you to do.


More information about the Digitalmars-d-learn mailing list