May 15, 2021

Are these identical? Or is there a different usage for the (T : something) form?

	auto opCast(T)() if (is(T == bool)) {
		return _obj !is null;
	}
	auto opCast(T : bool)() {
		return _obj !is null;
	}
May 15, 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?

	auto opCast(T)() if (is(T == bool)) {
		return _obj !is null;
	}

They are the same in this case.

>
	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.