On Tuesday, 1 October 2013 at 10:50:39 UTC, Joseph Rushton Wakeling wrote:
(1) Can someone please explain to me _in detail_ the mechanics of the code which identifies whether the first 2 template arguments have a common type?

I understand what it does, but not why/how it does it, if you get me :-)

    static if (is(typeof(true ? T[0].init : T[1].init) U))
    {
        alias CommonType!(U, T[2 .. $]) CommonType;
    }

a) Ternary operator returns value of type that can be used to store values of both condition branches. Don't know exact algorithm in compiler do determine this but it is likely based on presence of implicit conversion.

b) If it is impossible to use both types in ternary operator, no implicit conversions (and thus no common type) are possible. Thus it will result in compilation error and typeof() of whole expression will be invalid type, making is() return false.

c) If it is a valid expression, its type is CommonType and aliased to U. Then template is recursively called to find common type between U and rest of template parameter list.

d) In the end who;e CommonType template will be aliased to last such U in recursion chain.

(2) Same code -- why is it only necessary to check T[0].init : T[1].init and not vice versa? (Yes, you can tell I don't really understand the : operator properly:-)

It is not is(T : U) form but ternary operator.

(3) What would one have to implement in a library-defined type to enable T[0].init : T[1].init to evaluate to true? For example, to enable int and BigInt to be compatible?

It requires int to be implicitly convertible to BigInt. Don't know if we have tools to do it.

(4) Is there a good reason why there _shouldn't_ be a CommonType of (say) int and BigInt?

If it is possible to express it, common type of those should be BigInt.

Reply via email to