【问题标题】:Templates. Parameter pack expansion - remap types模板。参数包扩展——重映射类型
【发布时间】:2017-04-13 11:56:25
【问题描述】:

是否可以重新映射我们在参数包扩展期间获得的列表中的类型?

假设我们有下一个类型,我们想用作模板参数:

struct A {
    typedef char type;
};

struct B {
    typedef float type;
};

struct C {
    typedef void* type;
};

这是类模板:

template <typename... Args>
struct Foo {
    // after expansion should be
    // void bar(Arg0::type, Arg1::type, Arg2::type, ...)
    void bar(/* expand Args here */) {}
};

我们应该如何扩展Args 得到Foo::bar(Arg0::type, Arg1::type, Arg2::type, ...)

Foo<A, B, C> f;
// now there should be method void f::bar(A::type, B::type, C::type)
f.bar('a', 1.f, nullptr);

有可能吗?

【问题讨论】:

    标签: c++ c++11 templates variadic-templates


    【解决方案1】:

    是的,这很简单:

    template <typename... Args>
    struct Foo {
        void bar(typename Args::type... a) {}
    };
    

    [Live example]


    [基于意见]

    请注意,这是不使用复数命名参数包的一个很好的理由;参数包的所有用法实际上都是这样编写的,包名称代表包的一个单个元素,并且使用... 进行扩展。

    [/基于意见]

    【讨论】:

      猜你喜欢
      • 2021-12-28
      • 2014-12-17
      • 2019-01-15
      • 1970-01-01
      • 2014-10-30
      • 2021-03-05
      • 2015-05-21
      • 1970-01-01
      相关资源
      最近更新 更多