Compile-time variadic equality

Nordlöw per.nordlow at gmail.com
Fri Mar 9 19:25:22 UTC 2018


On Friday, 9 March 2018 at 19:24:03 UTC, Nordlöw wrote:
> I'm looking for a function (that probably should be placed in 
> std.meta) named something like `areEqual` that checks whether 
> all it's arguments are equal or not.
>
> Is there such as function already in Phobos?
>
> My usage is
>
> static if (allEqual!(staticMap!(ElementEncodingType, Rs)))
> {
>     // compare Rs byCodeUnit
> }

My current own implementation is

/** Returns: true iff all values $(D V) are the same.
     See also: 
http://forum.dlang.org/post/iflpslqgrixdjwrlqqvn@forum.dlang.org
     See also: 
http://forum.dlang.org/post/mheumktihihfsxxxapff@forum.dlang.org
*/
template allSame(V ...)
     if (isExpressions!V)
{
     static if (V.length <= 1)
     {
         enum allSame = true;
     }
     else static if (V.length & 1)
     {
         enum allSame = (V[$ - 1] == V[0] &&
                         V[0 .. $/2] == V[$/2 .. $-1] &&
                         allSame!(V[0 .. $/2]));
     }
     else
     {
         enum allSame = (V[0 .. $/2] == V[$/2 .. $] &&
                         allSame!(V[0 .. $/2]));
     }
}

Should this go into std.meta?


More information about the Digitalmars-d-learn mailing list