std.traits
Templates which extract information about types and symbols at compile time.
Copyright
Types 37
Parameters
T | The type to qualify |
Returns
T with the inout qualifier added.Parameters
T | The type to qualify |
Returns
T with the const qualifier added.Parameters
T | The type to qualify |
Returns
T with the shared qualifier added.Parameters
T | The type to qualify |
Returns
T with the inout and shared qualifiers added.Parameters
T | The type to qualify |
Returns
T with the const and shared qualifiers added.Parameters
T | The type to qualify |
Returns
T with the const, shared, and inout qualifiers added.Parameters
T | The type to qualify |
Returns
T with the immutable qualifier added.Alternate name for Parameters, kept for legacy compatibility.
Get a tuple of the storage classes of a function's parameters.
Parameters
func | function symbol or type of function, delegate, or pointer to function |
Returns
Alternate name for ParameterDefaults, kept for legacy compatibility.
Returns the FunctionAttribute mask for function func.
See Also
Determines what kind of variadic parameters function has.
Parameters
func | function symbol or type of function, delegate, or pointer to function |
Returns
Alternate name for Fields, kept for legacy compatibility.
Get a AliasSeq of all base classes of T, in decreasing order, followed by T's interfaces. TransitiveBaseTypeTuple!Object yields the empty type tuple.
Returns an alias to the template that T is an instance of. It will return void if a symbol without a template is given.
ditto
Returns a AliasSeq of the template arguments used to instantiate T.
ditto
Returns the target type of a pointer.
Alternate name for isExpressions, kept for legacy compatibility.
Get the Key type of an Associative Array.
Get the Value type of an Associative Array.
The parameter type deduced by IFTI when an expression of type T is passed as an argument to a template function.
For all types other than pointer and slice types, DeducedParameterType!T is the same as T. For pointer and slice types, it is T with the outer-most layer of qualifiers dropped.
ditto
Functions 6
T rvalueOf(T)(inout __InoutWorkaroundStruct = __InoutWorkaroundStruct.init) @propertyCreates an lvalue or rvalue of type `T` for `typeof(...)` and spec/traits`) purposes. No actual value is returned.A select(bool cond : true, A, B)(A a, lazy B b)Select one of two functions to run via template parameter.Variables 25
isImplicitlyConvertible = is(From : To)Is From implicitly convertible to To?
isQualifierConvertible = is(immutable From == immutable To) && is(From * : To *)Is From spec/const3 to To?
isBoolean = __traits(isUnsigned, T) && is(T : bool)Detect whether T is a built-in boolean type or enum of boolean base type.
isFloatingPoint = __traits(isFloating, T) && is(T : real)Detect whether T is a built-in floating point type.
See also: spec/traits
isScalarType = __traits(isScalar, T) && is(T : real)Detect whether T is a scalar type (a built-in numeric, character or boolean type).
See also: spec/traits
isBasicType = isScalarType!T || is(immutable T == immutable void)Detect whether T is a basic type (scalar type or void).
isSigned = __traits(isArithmetic, T) && !__traits(isUnsigned, T)
&& is(T : real)Detect whether T is a built-in signed numeric type.
isSomeString = is(immutable T == immutable C[], C) && (is(C == char) || is(C == wchar) || is(C == dchar))Detect whether T is one of the built-in string types.
The built-in string types are Char[], where Char is any of char, wchar or dchar, with or without qualifiers.
Static arrays of characters (like char[80]) are not considered built-in string types.
isNarrowString = is(immutable T == immutable C[], C) && (is(C == char) || is(C == wchar))Detect whether type T is a narrow string.
All arrays that use char, wchar, and their qualified versions are narrow strings. (Those include string and wstring).
isOrderingComparable = is(typeof((ref T a) => a < a ? 1 : 0))Detects whether T is a comparable type. Basic types and structs and classes that implement opCmp are ordering comparable.
isEqualityComparable = is(typeof((ref T a) => a == a ? 1 : 0))ditto
isStaticArray = __traits(isStaticArray, T)Detect whether type T is a static array.
See also: spec/traits
isArray = isStaticArray!T || isDynamicArray!TDetect whether type T is an array (static or dynamic; for associative arrays see isAssociativeArray).
isAssociativeArray = __traits(isAssociativeArray, T)Detect whether T is an associative array type
See also: spec/traits
isBuiltinType = is(BuiltinTypeOf!T) && !isAggregateType!TDetect whether type T is a builtin type.
isSIMDVector = is(T : __vector(V[N]), V, size_t N)Detect whether type T is a SIMD vector type.
isPointer = is(T == U *, U)Detect whether type T is a pointer.
isIterable = is(typeof({ foreach (elem; T.init) { } }))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 = !is(T == const) && !is(T == immutable) && !is(T == inout)Returns true if T is not const or immutable. Note that isMutable is true for string, or immutable(char)[], because the 'head' is mutable.
isInstanceOf = is(T == S!Args, Args...)Returns true if T is an instance of the template S.
isFunctionPointer = is(typeof(* T) == function)Detect whether symbol or type T is a function pointer.
isDelegate = is(typeof(T) == delegate) || is(T == delegate)Detect whether symbol or type T is a delegate.
isSomeFunction = is(T == return) ||
is(typeof(T) == return) ||
is(typeof(& T) == return)Detect whether symbol or type T is a function, a function pointer or a delegate.
Parameters
T | The type to check |
Returns
boolallSameType = {
static foreach (T; Ts[Ts.length > 1 .. $])
static if (!is(Ts[0] == T))
if (__ctfe) // Dodge the "statement is unreachable" warning
return false;
return true;
}()Returns
true iff all types Ts are the same.dip1000Enabled = is(typeof(& dip1000Test) : int function(int) @safe)Templates 100
Gives a template that can be used to apply the same attributes that are on the given type T. E.g. passing inout shared int will return SharedInoutOf.
Parameters
T | the type to check qualifiers from |
Returns
TGet the full package name for the given symbol.
Get the module name (including package) for the given symbol.
if (isCallable!func)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).
Note
To reduce template instantiations, consider instead usingtypeof(() { return func(args); } ()) if the argument types are known or
static if (is(typeof(func) Ret == return)) if only that basic test is needed.if (isCallable!func)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.
if (isCallable!func && variadicFunctionStyle!func == Variadic.no)Returns the number of arguments of function func. arity is undefined for variadic functions.
if (isCallable!func)ditto
Convert the result of spec/traits to ParameterStorageClass enums.
Parameters
Attribs | The return value of __traits(getParameterStorageClasses) |
Returns
enums.if (isCallable!func)Get, as a tuple, the identifiers of the parameters to a function symbol.
if (isCallable!func)Get, as a tuple, the default values of the parameters to a function symbol. If a parameter doesn't have the default value, void is returned instead.
if (isCallable!func)ditto
if (args.length > 0 && isCallable!(args[0])
&& allSatisfy!(isSomeString, typeof(args[1 .. $])))Checks whether a function has the given attributes attached.
Parameters
args | Function to check, followed by a variadic number of function attributes as strings |
Returns
true, if the function has the list of attributes attached and false otherwise.See Also
if (isCallable!func)true if func is @safe or @trusted.
true if func is @system.
if (isCallable!func)Determine the linkage attribute of the function.
Parameters
func | the function symbol, or the type of a function, delegate, or pointer to function |
Returns
if (isCallable!func)ditto
if (isCallable!func)Get the function type from a callable object func, or from a function pointer/delegate type.
Using builtin typeof on a property function yields the types of the property value, not of the property function itself. Still, FunctionTypeOf is able to obtain function types of properties.
Note
usually used for compile-time reflection purposes.
if (isFunctionPointer!T || isDelegate!T)Constructs a new function or delegate type with the same basic signature as the given one, but different attributes (including linkage).
This is especially useful for adding/removing attributes to/from types in generic code, where the actual type name cannot be spelt out.
Parameters
T | The base type. |
linkage | The desired linkage of the result type. |
attrs | The desired FunctionAttributes of the result type. |
if (is(T == function))Ditto
if (is(T == class))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)
Parameters
T | type to test |
Returns
true if T is a class nested inside another, with the conditions described above;
false otherwise
if (is(T == class) || is(T == struct) || is(T == union))Determines whether T has its own context pointer. T must be either class, struct, or union.
See also: spec/traits
Determines whether T or any of its representation types have a context pointer.
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, interface or union returns a tuple with one element T.
History
AliasSeq!(Interface) for interfaces prior to 2.097Get 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. Inherited fields (for classes) are not included. If T isn't a struct, class, interface or union, an expression tuple with an empty string is returned.
History
AliasSeq!"" for interfaces prior to 2.097Get the primitive types of the fields of a struct or class, in topological order.
Returns true if and only if T's representation includes at least one of the following:
- a raw pointer
U*andUis not immutable; - an array
U[]andUis not immutable; - a reference to a class or interface type
CandCis not immutable. - an associative array that is not immutable.
- a delegate.
Returns true if and only if T's representation includes at least one of the following:
- a raw pointer
U*; - an array
U[]; - a reference to a class type
C; - an associative array;
- a delegate;
- a [context pointer][isNested].
Returns true if and only if T's representation includes at least one of the following:
- a raw pointer
U*andUis not immutable or shared; - an array
U[]andUis not immutable or shared; - a reference to a class type
CandCis not immutable or shared. - an associative array that is not immutable or shared.
- a delegate that is not shared.
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.
A type S gets compiler-generated opAssign if it has an elaborate destructor.
Classes and unions never have elaborate assignments.
Note
hidden yet elaborate compiler generated assignment operator (unless explicitly disabled).
Whether the symbol represented by the string, member, exists and is a static member of T.
Parameters
T | Type containing symbol member. |
member | Name of symbol to test that resides in T. |
Returns
true iff member exists and is static.if (is(E == enum))Retrieves the members of an enumerated type enum E.
Parameters
E | An enumerated type. E may have duplicated values. |
Returns
E.
The members are arranged in the same order as declared in E. The name of the enum can be found by querying the compiler for the name of the identifier, i.e. __traits(identifier, EnumMembers!MyEnum[i]). For enumerations with unique values, to can also be used.
Note
to use EnumMembers to e.g. generate switch cases at compile-time, you should use the NoDuplicates template to avoid generating duplicate switch cases.
Note: Returned values are strictly typed with E. Thus, the following code does not work without the explicit cast: -------------------- enum E : int { a, b, c } int[] abc = cast(int[]) [ EnumMembers!E ]; -------------------- Cast is not necessary if the type of the variable is inferred. See the example below.
Get a AliasSeq of the base class and base interfaces of this class or interface. BaseTypeTuple!Object returns the empty type tuple.
if (is(T == class))Get a AliasSeq of all base classes of this class, in decreasing order. Interfaces are not included. BaseClassesTuple!Object yields the empty type tuple.
Parameters
T | The class or interface to search. |
Returns
AliasSeq of all interfaces directly or
indirectly inherited by this class or interface. Interfaces do not repeat if multiply implemented.
InterfacesTuple!Object yields an empty AliasSeq.
if (is(C == class) || is(C == interface))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.
if (is(T == class))Returns class instance alignment.
See also: spec/traits
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.
Parameters
T | The type to check |
Returns
AliasSeq with all possible target types of an implicit
conversion T.
If T is a class derived from Object, the result of
If the type is not a built-in value type or a class derived from Object, an empty AliasSeq is returned.
See Also
Parameters
T | The type to check Warning: This template is considered out-dated. It will be removed from Phobos in 2.107.0. Please use AllImplicitConversionTargets instead. |
Returns
AliasSeq with all possible target types of an implicit
conversion T.
If T is a class derived from Object, the result of
If the type is not a built-in value type or a class derived from Object, an empty AliasSeq is returned.
Note
language allows, eliminating all dangerous conversions. For example, ImplicitConversionTargets!double does not include float.
See Also
if (is(F == function) && is(G == function) ||
is(F == delegate) && is(G == delegate) ||
isFunctionPointer!F && isFunctionPointer!G)Determines whether the function type F is covariant with G, i.e., functions of the type F can override ones of the type G.
Detect whether T is a built-in integral type. Integral types are byte, ubyte, short, ushort, int, uint, long, ulong, cent, ucent, and enums with an integral type as its base type.
Parameters
T | type to test |
Returns
true if T is an integral typeNote
__traits(isIntegral)Detect whether T is a built-in numeric type (integral or floating point).
Detect whether T is a built-in unsigned numeric type.
Detect whether T is one of the built-in character types.
The built-in char types are any of char, wchar or dchar, with or without qualifiers.
in Phobos. For a function parameter to safely accept a type that implicitly converts to string as a string, the conversion needs to happen at the callsite; otherwise, the conversion is done inside the function, and in many cases, that means that local memory is sliced (e.g. if a static array is passed to the function, then it's copied, and the resulting dynamic array will be a slice of a local variable). So, if the resulting string escapes the function, the string refers to invalid memory, and accessing it would mean accessing invalid memory. As such, the only safe way for a function to accept types that implicitly convert to string is for the implicit conversion to be done at the callsite, and that can only occur if the parameter is explicitly typed as an array, whereas using isConvertibleToString in a template constraint would result in the conversion being done inside the function. As such, isConvertibleToString is inherently unsafe and is going to be deprecated.
Detect whether T is a struct, static array, or enum that is implicitly convertible to a string.
Detect whether type T is a string that will be autodecoded.
Given a type S that is one of:
const(char)[]const(wchar)[]
Type T can be one of:
S- implicitly convertible to
T - an enum with a base type
T - an aggregate with a base type
T
with the proviso that T cannot be a static array.
Parameters
T | type to be tested |
Returns
See Also:
isNarrowStringDetect whether type T is a dynamic array.
Detect whether type T is an aggregate type.
ditto
Check whether the tuple T is an expression tuple. An expression tuple only contains expressions.
See Also
Detect whether T is a callable object, which can be called with the function call operator (...).
Note
Implicit Function Template Instantiation is not attempted - see below.Determines if f is a function that requires a context pointer.
Parameters
f | The type to check Returns A bool |
Removes shared qualifier, if any, from type T.
Note that while immutable is implicitly shared, it is unaffected by Unshared. Only explict shared is removed.
Copies type qualifiers from FromType to ToType.
Supported type qualifiers:
constinoutimmutableshared
Returns the type of ToType with the "constness" of FromType. A type's constness refers to whether it is const, immutable, or inout. If FromType has no constness, the returned type will be the same as ToType.
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.
Strips off all enums from type T.
Parameters
T | A built in integral or vector type. |
Returns
T with the
same type qualifiers.
If T is not a integral or vector, a compile-time error is given.
if (T.length >= 1)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.
Returns the corresponding signed type for T. T must be a numeric integral type, otherwise a compile-time error occurs.
if (isNumeric!T || isSomeChar!T || isBoolean!T)Returns the most negative value of the numeric type T.
if (isScalarType!T)Get the type that a scalar type T will promote to in multi-term arithmetic expressions.
if (T.length == 2)Aliases itself to T[0] if the boolean condition is true and to T[1] otherwise.
Gets the matching spec/attribute from the given symbol.
If the UDA is a type, then any UDAs of the same type on the symbol will match. If the UDA is a template for a type, then any UDA which is an instantiation of that template will match. And if the UDA is a value, then any UDAs on the symbol which are equal to that value will match.
See Also
Parameters
symbol | The aggregate type or module to search |
attribute | The user-defined attribute to search for |
Returns
symbol that have the given UDA attribute.Note
nested structs or unions.
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.
Returns
true if X is a function, false otherwiseSee Also
Detect whether X is a final method or class.
Returns
true if X is final, false otherwise