This is the homepage of Peter Alexander. I am currently at Facebook working on AR/VR. Any opinions are my own.
Recent Posts
- Single-Threaded Parallelism
- unique_ptr Type Erasure
- The Condenser Part 1
- Cube Vertex Numbering
- Range-Based Graph Search in D
All Posts
Links
github.com/Poita@Poita_
Atom Feed
D Tip - Testing with TypeTuple
When testing a function, it’s usually a good idea to test it with a range
of different inputs. To do this, you can easily use a for
loop over an array
of input values, but what if your input is a type, as it often is with
template code?
The D Programming Language allows you to iterate over a TypeTuple
, so
all you need to do is declare a tuple of all the types you want to test, and
iterate over them in the normal way:
You might wonder what this compiles to. After all, the body of the loop varies
with T
, so the generated code must also vary on each iteration. How does the
compiler handle this?
The answer is that the loop is completely unrolled. The code above is literally the same as:
For this reason, you might want to keep an eye on the size of your TypeTuple
s,
to avoid code bloat.