VariantPointerTo

A typed pointer to a variant of Types, packed into a word (size_t).

Constructors

this
this(T* value)
this(typeof(null) value)

Construction from value.

Members

Enums

canStorePointerTo
eponymoustemplate canStorePointerTo(T)

Is true iff a pointer to a T can be stored.

Functions

opAssign
auto ref opAssign(T* that)
auto ref opAssign(typeof(null) that)

Assignment from that.

Examples

1 import std.meta : AliasSeq;
2 
3 alias Types = AliasSeq!(byte, short, int, long,
4                         ubyte, ushort, uint, ulong,
5                         float, double, real,
6                         char, wchar, dchar);
7 
8 alias V = VariantPointerTo!Types;
9 
10 V v;
11 assert(v.isNull);
12 v = null;
13 assert(v.isNull);
14 assert(!v);
15 
16 foreach (T; Types)
17 {
18     static assert(!__traits(compiles, { T[] a; v = &a; }));
19     static assert(!__traits(compiles, { v.peek!(T[]*); }));
20 
21     // assignment from stack pointer
22     T a = 73;
23     T a_ = 73;
24     v = &a;
25     assert(v == &a);
26     assert(v != &a_);
27     assert(v);
28     foreach (U; Types)
29     {
30         static if (is(T == U))
31         {
32             assert(v.peek!U);
33             assert(*(v.peek!U) == a);
34         }
35         else
36         {
37             assert(!v.peek!U);
38         }
39     }
40 
41     // assignment from heap pointer
42     T* b = new T;
43     T* b_ = new T;
44     *b = 73;
45     *b_ = 73;
46     v = b;
47     assert(v == b);
48     assert(v != b_);
49     assert(v);
50     foreach (U; Types)
51     {
52         static if (is(T == U))
53         {
54             assert(v.peek!U);
55             assert(*(v.peek!U) == *b);
56         }
57         else
58         {
59             assert(!v.peek!U);
60         }
61     }
62 
63 }

See Also

std.bitmanip.taggedPointer.

Meta