【问题标题】:Creating a typetuple from a variadic template argument list in D从 D 中的可变参数模板参数列表创建类型元组
【发布时间】:2011-10-30 21:25:38
【问题描述】:

我有一个以下形式的模板结构:

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

此外,我还有另一个容器结构,其中包含一堆 Command 结构:

struct CommandList(Command...) {
}

我想做的是,通过模板和/或 mixins,在 CommandList 中创建一个 TypeTuple 别名,其中包含每个模板 Command 参数的 CommandTypes,按顺序排列。例如,我希望发生这样的事情:

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...
}

这是否可行,如果可以,最好的方法是什么?

【问题讨论】:

    标签: templates d template-mixins


    【解决方案1】:

    这能满足你的需要吗?

    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-22
      • 1970-01-01
      相关资源
      最近更新 更多