phobos.sys.traits

Templates which extract information about types and symbols at compile time.

In the context of phobos.sys.traits, a "trait" is a template which provides information about a type or symbol. Most traits evaluate to true or false, telling the code using it whether the given arguments match / have that specific trait (e.g. whether the given type is a dynamic array or whether the given function is @safe). However, some traits may provide other kinds of information about a type (e.g. the trait could evaluate to the base type for an enum type, or it could strip const from the type to provide the mutable version of that type).

These traits are then used primarily in template constraints so that they can test that the template arguments meet the criteria required by those templates, though they can be useful in a variety of compile-time contexts (e.g. the condition of a static if).

Note that unless otherwise specified, the isXXXX and hasXXX traits in this module are checking for exact matches, so base types (e.g. with enums) and other implicit conversions do not factor into whether such traits are true or false. The type itself is being checked, not what it can be converted to.

This is because these traits are often used in templated constraints, and having a type pass a template constraint based on an implicit conversion but then not have the implicit conversion actually take place (which it won't unless the template does something to force it internally) can lead to either compilation errors or subtle behavioral differences - and even when the conversion is done explicitly within a templated function, since it's not done at the call site, it can still lead to subtle bugs in some cases (e.g. if slicing a static array is involved).

So, it's typically best to be explicit and clear about a template constraint accepting any kind of implicit conversion rather than having it buried in a trait where programmers stand a good chance of using the trait without realizing that enums might pass based on their base type - or that a type might pass based on some other implicit conversion.

Regardless of what a trait is testing for, the documentation strives to be very clear about what the trait does, and of course, the names do try to make it clear as well - though obviously, only so much information can be put into a name, and some folks will misintrepret some symbols no matter how well they're named. So, please be sure that you clearly understand what these traits do when using them, since messing up template constraints can unfortunately be a great way to introduce subtle bugs into your program. Either way, of course, unit tests are your friends.

CategoryTemplates
Categories of typesisAggregateType isDynamicArray isFloatingPoint isInstantiationOf isInteger isNumeric isPointer isSignedInteger isStaticArray isUnsignedInteger
Aggregate Type traitsEnumMembers
Traits testing for type conversionsisImplicitlyConvertible isQualifierConvertible
Traits for comparisonsisEqual isSameSymbol isSameType
Aggregate Type TraitsFieldNames FieldSymbols FieldTypes
General TypesKeyType OriginalType ValueType
Traits for removing type qualfiersUnconst Unshared Unqualified
Type ConstructorsConstOf ImmutableOf InoutOf SharedOf
MisclvalueOf rvalueOf

Members

Aliases

ConstOf
alias ConstOf(T) = T

Applies const to the given type.

ImmutableOf
alias ImmutableOf(T) = T

Applies immutable to the given type.

InoutOf
alias InoutOf(T) = T

Applies inout to the given type.

KeyType
alias KeyType(V : V[K], K) = K

Takes a type which is an associative array and evaluates to the type of the keys in that associative array.

SharedOf
alias SharedOf(T) = T

Applies shared to the given type.

ValueType
alias ValueType(V : V[K], K) = V

Takes a type which is an associative array and evaluates to the type of the values in that associative array.

Enums

isAggregateType
eponymoustemplate isAggregateType(T)

Whether the given type is an "aggregate type" - i.e. a struct, class, interface, or union.

isDynamicArray
eponymoustemplate isDynamicArray(T)

Whether the given type is a dynamic array (or what is sometimes referred to as a slice, since a dynamic array in D is a slice of memory).

isEqual
eponymoustemplate isEqual(alias lhs, alias rhs)

Whether the given values are equal per ==.

isFloatingPoint
eponymoustemplate isFloatingPoint(T)

Whether the given type is one of the built-in floating-point types, ignoring all qualifiers.

isImplicitlyConvertible
eponymoustemplate isImplicitlyConvertible(From, To)

Whether the type From is implicitly convertible to the type To.

isInteger
eponymoustemplate isInteger(T)

Whether the given type is one of the built-in integer types, ignoring all qualifiers.

isNumeric
eponymoustemplate isNumeric(T)

Whether the given type is one of the built-in numeric types, ignoring all qualifiers. It's equivalent to isInteger!T || isFloatingPoint!T, but it only involves a single template instantation instead of two.

isPointer
eponymoustemplate isPointer(T)

Whether the given type is a pointer.

isQualifierConvertible
eponymoustemplate isQualifierConvertible(From, To)

Whether From is qualifier-convertible to To.

isSameSymbol
eponymoustemplate isSameSymbol(alias lhs, alias rhs)

Whether the given symbols are the same symbol.

isSameType
eponymoustemplate isSameType(T, U)

Whether the given types are the same type.

isSignedInteger
eponymoustemplate isSignedInteger(T)

Whether the given type is one of the built-in signed integer types, ignoring all qualifiers.

isStaticArray
eponymoustemplate isStaticArray(T)

Whether type T is a static array.

isUnsignedInteger
eponymoustemplate isUnsignedInteger(T)

Whether the given type is one of the built-in unsigned integer types, ignoring all qualifiers.

Properties

lvalueOf
T lvalueOf [@property getter]

Creates an lvalue or rvalue of type T to be used in conjunction with is(typeof(...)) or __traits(compiles, ...).

rvalueOf
T rvalueOf [@property getter]

Creates an lvalue or rvalue of type T to be used in conjunction with is(typeof(...)) or __traits(compiles, ...).

Templates

EnumMembers
template EnumMembers(E)

Evaluates to an AliasSeq containing the members of an enum type.

FieldNames
template FieldNames(T)

Evaluates to an AliasSeq of the names (as strings) of the member variables of an aggregate type (i.e. a struct, class, interface, or union).

FieldSymbols
template FieldSymbols(T)

Evaluates to an AliasSeq of the symbols for the member variables of an aggregate type (i.e. a struct, class, interface, or union).

FieldTypes
template FieldTypes(T)

Evaluates to an AliasSeq of the types of the member variables of an aggregate type (i.e. a struct, class, interface, or union).

OriginalType
template OriginalType(T)

Evaluates to the original / ultimate base type of an enum type - or for non-enum types, it evaluates to the type that it's given.

Unconst
template Unconst(T)

Removes the outer layer of const, inout, or immutable from type T.

Unqualified
template Unqualified(T)

Removes the outer layer of all type qualifiers from type T - this includes shared.

Unshared
template Unshared(T)

Removes the outer layer of shared from type T.

isEqual
template isEqual(alias lhs)

Whether the given values are equal per ==.

isImplicitlyConvertible
template isImplicitlyConvertible(From)

Whether the type From is implicitly convertible to the type To.

isInstantiationOf
template isInstantiationOf(alias Template, T)
template isInstantiationOf(alias Template, alias Symbol)
template isInstantiationOf(alias Template)

Evaluates to true if the given type or symbol is an instantiation of the given template.

isQualifierConvertible
template isQualifierConvertible(From)

Whether From is qualifier-convertible to To.

isSameSymbol
template isSameSymbol(alias lhs)

Whether the given symbols are the same symbol.

isSameType
template isSameType(T)

Whether the given types are the same type.

Meta

Authors

Jonathan M Davis Walter Bright, Tomasz Stachowiak (isExpressions), Andrei Alexandrescu, Shin Fujishiro, Robert Clipsham, David Nadlinger, Kenji Hara, Shoichi Kato