templatesdtemplate-mixins

Creating a typetuple from a variadic template argument list in D


I have a templated struct of the following form:

struct Command(T) {
    alias T CommandType;
    // ...
}

In addition, I have another container struct that holds a bunch of these Command structs:

struct CommandList(Command...) {
}

What I'd like to do is, via templates and/or mixins, create a TypeTuple alias in CommandList which contains the CommandTypes, in order, of each of the template Command arguments. For example, I'd like for something like this to happen:

struct CommandList(Command!int, Command!long, Command!string, Command!float) {
    alias TypeTuple!(int, long, string, float) CommandListType; // This would be dynamically generated by templates/mixins...
}

Would this be possible to do, and if so, what is the best approach?


Solution

  • Does this do what you need?

    import std.typetuple;
    
    struct Command(T) { alias T CommandType; }
    template getCommandType(T) { alias T.CommandType getCommandType; }
    
    struct CommandList(Command...) {
        alias staticMap!(getCommandType, Command) CommandListType;
    }
    
    pragma(msg, CommandList!(Command!int, Command!long).CommandListType);