And

Combines multiple template predicates into a single template predicate using logical AND - i.e. for the resulting predicate to be true with a particular argument, all of the predicates must be true with that argument.

Evaluation is not short-circuited if a false result is encountered; the template predicate must be instantiable with all the elements.

template And (
Preds...
) {}

Examples

import phobos.sys.traits : isNumeric;

template isSameSize(size_t size)
{
    enum isSameSize(T) = T.sizeof == size;
}

alias is32BitNumeric = And!(isNumeric, isSameSize!4);

static assert(!is32BitNumeric!short);
static assert( is32BitNumeric!int);
static assert(!is32BitNumeric!long);
static assert( is32BitNumeric!float);
static assert(!is32BitNumeric!double);
static assert(!is32BitNumeric!(int*));

// An empty sequence of predicates always yields true.
alias alwaysTrue = And!();
static assert(alwaysTrue!int);

Predicates with multiple parameters are also supported. However, the number of parameters must match.

import phobos.sys.traits : isImplicitlyConvertible, isInteger, isSameType;

alias isOnlyImplicitlyConvertible
    = And!(Not!isSameType, isImplicitlyConvertible);

static assert( isOnlyImplicitlyConvertible!(int, long));
static assert(!isOnlyImplicitlyConvertible!(int, int));
static assert(!isOnlyImplicitlyConvertible!(long, int));

static assert( isOnlyImplicitlyConvertible!(string, const(char)[]));
static assert(!isOnlyImplicitlyConvertible!(string, string));
static assert(!isOnlyImplicitlyConvertible!(const(char)[], string));

// Mismatched numbers of parameters.
alias doesNotWork = And!(isInteger, isImplicitlyConvertible);
static assert(!__traits(compiles, doesNotWork!int));
static assert(!__traits(compiles, doesNotWork!(int, long)));

See Also

Meta