View source code
							
							
						
								Display the source code in std/variant.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.
							
						Struct std.variant.VariantN
Back-end type seldom used directly by user
 code. Two commonly-used types using VariantN are:
						
				struct VariantN(ulong maxDataSize, AllowedTypesParam...)
				;
						
					
				- Algebraic: A closed discriminated union with a limited type universe (e.g.,- Algebraic!(int, double, string)only accepts these three types and rejects anything else).
- Variant: An open discriminated union allowing an unbounded set of types. If any of the types in the- Variantare larger than the largest built-in type, they will automatically be boxed. This means that even large types will only be the size of a pointer within the- Variant, but this also implies some overhead.- Variantcan accommodate all primitive types and all user-defined types.
 Both Algebraic and Variant share  VariantN's interface. (See their respective documentations below.)
 VariantN is a discriminated union type parameterized
 with the largest size of the types stored (maxDataSize)
 and with the list of allowed types (AllowedTypes). If
 the list is empty, then any type up of size up to  maxDataSize (rounded up for alignment) can be stored in a
 VariantN object without being boxed (types larger
 than this will be boxed).
Constructors
| Name | Description | 
|---|---|
| this | Constructs a VariantNvalue given an argument of a
 generic type. Statically rejects disallowed types. | 
| this | Allows assignment from a subset algebraic type | 
Properties
| Name | Type | Description | 
|---|---|---|
| coerce[get] | T | Returns the value stored in the VariantNobject,
 explicitly converted (coerced) to the requested type T. IfTis a string type, the value is formatted as
 a string. If theVariantNobject is a string, a
 parse of the string to typeTis attempted. If a
 conversion is not possible, throws a VariantException. | 
| convertsTo[get] | bool | Returns trueif and only if theVariantNobject holds an object implicitly convertible to typeT.
 Implicit convertibility is defined as per
 ImplicitConversionTargets. | 
| get[get] | inout(T) | Returns the value stored in the VariantNobject, either by specifying the
    needed type or the index in the list of allowed types. The latter overload
    only applies to bounded variants (e.g.Algebraic). | 
| hasValue[get] | bool | Returns true if and only if the VariantNobject
 holds a valid value (has been initialized with, or assigned
 from, a valid value). | 
| length[get] | size_t | If the VariantNcontains an (associative) array,
 returns the length of that array. Otherwise, throws an
 exception. | 
| peek[get] | inout(T)* | If the VariantNobject holds a value of the
 exact typeT, returns a pointer to that
 value. Otherwise, returnsnull. In cases
 whereTis statically disallowed, peekwill not compile. | 
| type[get] | TypeInfo | Returns the typeidof the currently held value. | 
Methods
Aliases
| Name | Description | 
|---|---|
| AllowedTypes | The list of allowed types. If empty, any type is allowed. | 
Example
alias Var = VariantN!(maxSize!(int, double, string));
Var a; // Must assign before use, otherwise exception ensues
// Initialize with an integer; make the type int
Var b = 42;
writeln(bExample
can also assign arrays
alias Var = VariantN!(maxSize!(int[]));
Var a = new int[42];
writeln(aExample
Can also assign class values
alias Var = VariantN!(maxSize!(int*)); // classes are pointers
Var a;
class Foo {}
auto foo = new Foo;
a = foo;
assert(*aAuthors
License
					Copyright © 1999-2018 by the D Language Foundation | Page generated by ddox.