View source code
Display the source code in std/traits.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Module std.traits

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

Category Templates
Symbol Name traits fullyQualifiedName moduleName packageName
Function traits isFunction arity functionAttributes hasFunctionAttributes functionLinkage FunctionTypeOf isSafe isUnsafe isFinal ParameterDefaults ParameterIdentifierTuple ParameterStorageClassTuple Parameters ReturnType SetFunctionAttributes variadicFunctionStyle
Aggregate Type traits BaseClassesTuple BaseTypeTuple classInstanceAlignment EnumMembers FieldNameTuple Fields hasAliasing hasElaborateAssign hasElaborateCopyConstructor hasElaborateDestructor hasIndirections hasMember hasStaticMember hasNested hasUnsharedAliasing InterfacesTuple isInnerClass isNested MemberFunctionsTuple RepresentationTypeTuple TemplateArgsOf TemplateOf TransitiveBaseTypeTuple
Type Conversion CommonType ImplicitConversionTargets CopyTypeQualifiers CopyConstness isAssignable isCovariantWith isImplicitlyConvertible
SomethingTypeOf rvalueOf lvalueOf InoutOf ConstOf SharedOf SharedInoutOf SharedConstOf ImmutableOf QualifierOf
Categories of types allSameType ifTestable isType isAggregateType isArray isAssociativeArray isAutodecodableString isBasicType isBoolean isBuiltinType isCopyable isDynamicArray isEqualityComparable isFloatingPoint isIntegral isNarrowString isConvertibleToString isNumeric isOrderingComparable isPointer isScalarType isSigned isSIMDVector isSomeChar isSomeString isStaticArray isUnsigned
Type behaviours isAbstractClass isAbstractFunction isCallable isDelegate isExpressions isFinalClass isFinalFunction isFunctionPointer isInstanceOf isIterable isMutable isSomeFunction isTypeTuple
General Types ForeachType KeyType Largest mostNegative OriginalType PointerTarget Signed Unqual Unsigned ValueType Promoted
Misc mangledName Select select
User-Defined Attributes hasUDA getUDAs getSymbolsByUDA

Functions

Name Description
lvalueOf Creates an lvalue or rvalue of type T for typeof(...) and _traits(compiles, ...) purposes. No actual value is returned.
rvalueOf Creates an lvalue or rvalue of type T for typeof(...) and traits(compiles, ...) purposes. No actual value is returned.
select If cond is true, returns a without evaluating b. Otherwise, returns b without evaluating a.

Enums

Name Description
FunctionAttribute Returns the FunctionAttribute mask for function func.
ParameterStorageClass Get tuple, one per function parameter, of the storage classes of the parameters.
Variadic Determines what kind of variadic parameters function has.

Templates

Name Description
allSameType
BaseClassesTuple Get a AliasSeq of all base classes of this class, in decreasing order. Interfaces are not included. BaseClassesTuple!Object yields the empty type tuple.
BaseTypeTuple Get a AliasSeq of the base class and base interfaces of this class or interface. BaseTypeTuple!Object returns the empty type tuple.
CommonType Get the type that all types can be implicitly converted to. Useful e.g. in figuring out an array type from a bunch of initializing values. Returns void if passed an empty list, or if the types have no common type.
FieldNameTuple Get as an expression tuple the names of the fields of a struct, class, or union. This consists of the fields that take up memory space, excluding the hidden fields like the virtual function table pointer or a context pointer for nested types. If T isn't a struct, class, or union returns an expression tuple with an empty string.
Fields Get as a tuple the types of the fields of a struct, class, or union. This consists of the fields that take up memory space, excluding the hidden fields like the virtual function table pointer or a context pointer for nested types. If T isn't a struct, class, or union returns a tuple with one element T.
fullyQualifiedName Get the fully qualified name of a type or a symbol. Can act as an intelligent type/symbol to string converter.
FunctionTypeOf Get the function type from a callable object func.
getSymbolsByUDA Gets all symbols within symbol that have the given user-defined attribute. This is not recursive; it will not search for symbols within symbols such as nested structs or unions.
hasAliasing Returns true if and only if T's representation includes at least one of the following:
  1. a raw pointer U* and U is not immutable;
  2. an array U[] and U is not immutable;
  3. a reference to a class or interface type C and C is not immutable.
  4. an associative array that is not immutable.
  5. a delegate.
hasElaborateAssign True if S or any type directly embedded in the representation of S defines an elaborate assignment. Elaborate assignments are introduced by defining opAssign(typeof(this)) or opAssign(ref typeof(this)) for a struct or when there is a compiler-generated opAssign.
hasElaborateCopyConstructor True if S or any type embedded directly in the representation of S defines an elaborate copy constructor. Elaborate copy constructors are introduced by defining this(this) for a struct.
hasElaborateDestructor True if S or any type directly embedded in the representation of S defines an elaborate destructor. Elaborate destructors are introduced by defining ~this() for a struct.
hasIndirections Returns true if and only if T's representation includes at least one of the following:
  1. a raw pointer U*;
  2. an array U[];
  3. a reference to a class type C.
  4. an associative array.
  5. a delegate.
hasNested Determines whether T or any of its representation types have a context pointer.
hasStaticMember Whether the symbol represented by the string, member, exists and is a static member of T.
hasUnsharedAliasing Returns true if and only if T's representation includes at least one of the following:
  1. a raw pointer U* and U is not immutable or shared;
  2. an array U[] and U is not immutable or shared;
  3. a reference to a class type C and C is not immutable or shared.
  4. an associative array that is not immutable or shared.
  5. a delegate that is not shared.
ImplicitConversionTargets Returns a tuple with all possible target types of an implicit conversion of a value of type T.
InterfacesTuple Get a AliasSeq of all interfaces directly or indirectly inherited by this class or interface. Interfaces do not repeat if multiply implemented. InterfacesTuple!Object yields the empty type tuple.
isCallable Detect whether T is a callable object, which can be called with the function call operator (...).
isCovariantWith Determines whether the function type F is covariant with G, i.e., functions of the type F can override ones of the type G.
isDelegate Detect whether symbol or type T is a delegate.
isExpressions Check whether the tuple T is an expression tuple. An expression tuple only contains expressions.
isFinal Detect whether X is a final method or class.
isFunction Detect whether symbol or type X is a function. This is different that finding if a symbol is callable or satisfying is(X == function), it finds specifically if the symbol represents a normal function declaration, i.e. not a delegate or a function pointer.
isFunctionPointer Detect whether symbol or type T is a function pointer.
isInnerClass Determines whether T is a class nested inside another class and that T.outer is the implicit reference to the outer class (i.e. outer has not been used as a field or method name)
isSomeFunction Detect whether symbol or type T is a function, a function pointer or a delegate.
isTypeTuple Check whether the tuple T is a type tuple. A type tuple only contains types.
Largest Returns the largest type, i.e. T such that T.sizeof is the largest. If more than one type is of the same size, the leftmost argument of these in will be returned.
MemberFunctionsTuple Returns a tuple of non-static functions with the name name declared in the class or interface C. Covariant duplicates are shrunk into the most derived one.
moduleName Get the module name (including package) for the given symbol.
mostNegative Returns the most negative value of the numeric type T.
packageName Get the full package name for the given symbol.
Parameters Get, as a tuple, the types of the parameters to a function, a pointer to function, a delegate, a struct with an opCall, a pointer to a struct with an opCall, or a class with an opCall.
ParameterStorageClassTuple Get tuple, one per function parameter, of the storage classes of the parameters.
QualifierOf Get qualifier template from the given type T
RepresentationTypeTuple Get the primitive types of the fields of a struct or class, in topological order.
ReturnType Get the type of the return value from a function, a pointer to function, a delegate, a struct with an opCall, a pointer to a struct with an opCall, or a class with an opCall. Please note that ref is not part of a type, but the attribute of the function (see template functionAttributes).
SetFunctionAttributes Constructs a new function or delegate type with the same basic signature as the given one, but different attributes (including linkage).
TransitiveBaseTypeTuple Get a AliasSeq of all base classes of T, in decreasing order, followed by T's interfaces. TransitiveBaseTypeTuple!Object yields the empty type tuple.
Unqual Removes all qualifiers, if any, from type T.

Enum values

Name Type Description
arity Returns the number of arguments of function func. arity is undefined for variadic functions.
extractParameterStorageClassFlags Convert string tuple Attribs to ParameterStorageClass bits
functionAttributes Returns the FunctionAttribute mask for function func.
functionLinkage Determine the linkage attribute of the function.
hasFunctionAttributes Checks whether a function has the given attributes attached.
hasMember Yields true if and only if T is an aggregate that defines a symbol called name.
hasUDA Determine if a symbol has a given user-defined attribute.
ifTestable
isAbstractClass Detect whether T is an abstract class.
isAbstractFunction Detect whether T is an abstract function.
isAggregateType Detect whether type T is an aggregate type.
isArray Detect whether type T is an array (static or dynamic; for associative arrays see isAssociativeArray).
isAssignable Returns true iff a value of type Rhs can be assigned to a variable of type Lhs.
isAssociativeArray Detect whether T is an associative array type
isAutodecodableString Detect whether type T is a string that will be autodecoded.
isBasicType Detect whether T is a basic type (scalar type or void).
isBoolean Detect whether T is a built-in boolean type.
isBuiltinType Detect whether type T is a builtin type.
isConvertibleToString Detect whether T is a struct, static array, or enum that is implicitly convertible to a string.
isCopyable Determines whether the type S can be copied. If a type cannot be copied, then code such as MyStruct x; auto y = x; will fail to compile. Copying for structs can be disabled by using @disable this(this).
isDynamicArray Detect whether type T is a dynamic array.
isEqualityComparable Detects whether T is a comparable type. Basic types and structs and classes that implement opCmp are ordering comparable.
isFinalClass Detect whether T is a final class.
isFinalFunction Detect whether T is a final function.
isFloatingPoint Detect whether T is a built-in floating point type.
isImplicitlyConvertible Is From implicitly convertible to To?
isInstanceOf Returns true if T is an instance of the template S.
isIntegral Detect whether T is a built-in integral type. Types bool, char, wchar, and dchar are not considered integral.
isIterable Returns true if T can be iterated over using a foreach loop with a single loop variable of automatically inferred type, regardless of how the foreach loop is implemented. This includes ranges, structs/classes that define opApply with a single loop variable, and builtin dynamic, static and associative arrays.
isMutable Returns true if T is not const or immutable. Note that isMutable is true for string, or immutable(char)[], because the 'head' is mutable.
isNarrowString Detect whether type T is a narrow string.
isNested Determines whether T has its own context pointer. T must be either class, struct, or union.
isNestedFunction Determines whether function f requires a context pointer.
isNumeric Detect whether T is a built-in numeric type (integral or floating point).
isOrderingComparable Detects whether T is a comparable type. Basic types and structs and classes that implement opCmp are ordering comparable.
isPointer Detect whether type T is a pointer.
isSafe true if func is @safe or @trusted.
isScalarType Detect whether T is a scalar type (a built-in numeric, character or boolean type).
isSigned Detect whether T is a built-in signed numeric type.
isSIMDVector Detect whether type T is a SIMD vector type.
isSomeChar Detect whether T is one of the built-in character types.
isSomeString Detect whether T is one of the built-in string types.
isStaticArray Detect whether type T is a static array.
isType Detect whether X is a type. Analogous to is(X). This is useful when used in conjunction with other templates, e.g. allSatisfy!(isType, X).
isUnsafe true if func is @system.
isUnsigned Detect whether T is a built-in unsigned numeric type.
mangledName Returns the mangled name of symbol or type sth.
variadicFunctionStyle Determines what kind of variadic parameters function has.

Aliases

Name Type Description
classInstanceAlignment maxAlignment!(void*,typeof(T.tupleof)) Returns class instance alignment.
ConstOf const(T) Add the const qualifier to the given type T.
CopyConstness Unshared!(CopyTypeQualifiers!(FromType,ToType)) Returns the type of Target with the "constness" of Source. A type's constness refers to whether it is const, immutable, or inout. If source has no constness, the returned type will be the same as Target.
CopyTypeQualifiers ModifyTypePreservingTQ!(T,FromType) Copies type qualifiers from FromType to ToType.
EnumMembers EnumSpecificMembers!(__traits(allMembers,E)) Retrieves the members of an enumerated type enum E.
FieldTypeTuple Alternate name for Fields, kept for legacy compatibility.
ForeachType ReturnType!(typeof((inoutintx=0){foreach(elem;T.init){returnelem;}assert(0);})) Returns the inferred type of the loop variable when a variable of type T is iterated over using a foreach loop with a single loop variable and automatically inferred return type. Note that this may not be the same as std.range.ElementType!Range in the case of narrow strings, or if T has both opApply and a range interface.
getUDAs Filter!(isDesiredUDA,__traits(getAttributes,symbol)) Gets the matching user-defined attributes from the given symbol.
ImmutableOf immutable(T) Add the immutable qualifier to the given type T.
InoutOf inout(T) Add the inout qualifier to the given type T.
isExpressionTuple Alternate name for isExpressions, kept for legacy compatibility.
KeyType K Get the Key type of an Associative Array.
OriginalType ModifyTypePreservingTQ!(Impl,T) Strips off all enums from type T.
ParameterDefaults Impl!() Get, as a tuple, the default value of the parameters to a function symbol. If a parameter doesn't have the default value, void is returned instead.
ParameterDefaultValueTuple Alternate name for ParameterDefaults, kept for legacy compatibility.
ParameterIdentifierTuple Impl!() Get, as a tuple, the identifiers of the parameters to a function symbol.
ParameterTypeTuple Alternate name for Parameters, kept for legacy compatibility.
PointerTarget T Returns the target type of a pointer.
CopyTypeQualifiers!(T,typeof(T.init+T.init)) Get the type that a scalar type T will promote to in multi-term arithmetic expressions.
Select Alias!(T[!condition]) Aliases itself to T[0] if the boolean condition is true and to T[1] otherwise.
SetFunctionAttributes FunctionTypeOf!(SetFunctionAttributes!(T*,linkage,attrs)) Constructs a new function or delegate type with the same basic signature as the given one, but different attributes (including linkage).
SharedConstOf const(shared(T)) Add the shared and const qualifiers to the given type T.
SharedInoutOf inout(shared(T)) Add the shared and inout qualifiers to the given type T.
SharedOf shared(T) Add the shared qualifier to the given type T.
Signed ModifyTypePreservingTQ!(Impl,OriginalType!T) Returns the corresponding signed type for T. T must be a numeric integral type, otherwise a compile-time error occurs.
TemplateArgsOf Args Returns a AliasSeq of the template arguments used to instantiate T.
TemplateOf Base Returns an alias to the template that T is an instance of.
Unsigned ModifyTypePreservingTQ!(Impl,OriginalType!T) Returns the corresponding unsigned type for T. T must be a numeric integral type, otherwise a compile-time error occurs.
ValueType V Get the Value type of an Associative Array.

Authors

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

License

Boost License 1.0.

Comments