Reimplementing the bulk of std.meta iteratively
Adam D. Ruppe
destructionator at gmail.com
Sun Sep 27 00:27:50 UTC 2020
On Sunday, 27 September 2020 at 00:10:33 UTC, Stefan Koch wrote:
> This example on the other hand works:
Here's another potential implementation:
---
template sortBySize(T...) {
string code() {
// step one: gather the data in the template shim
static struct Item {
size_t idx;
size_t size;
}
Item[] items;
foreach(idx, t; T)
items ~= Item(idx, t.sizeof);
// step two: process the data with normal CTFE
import std.algorithm;
items.sort!((a, b) => a.size < b.size);
// step three: build the code for the template to
bring it back
// this would be a fine candidate for a helper
function
string code;
foreach(item; items) {
if(code.length) code ~= ", ";
code ~= "T[" ~ cast(char)(item.idx + '0')
~ "]";
}
return "AliasSeq!(" ~ code ~ ")";
}
import std.meta;
alias sortBySize = mixin(code());
}
pragma(msg, sortBySize!(ushort, int, byte));
---
(byte, ushort, int)
There's several optimizations here, preallocating, factoring out
of the template, not using std.algorithm, you know those tricks
already.
I'm not in love with it but there is some potential in the
concept in today's D.
More information about the Digitalmars-d
mailing list