variadic模板仅使用类型参数

can*_*las 3 c++

我想做这样的事情:

#include <iostream>

class a {

public:
    a() : i(2) {}

    template <typename ...ts>
    void exec() {
        f<ts...>();
        std::cout << "a::()" << std::endl;
    }

    int i;
private:

    template <typename t>
    void f() {
        i += t::i;
    }

    template <typename t, typename ...ts>
    void f() {
        f<t>();
        f<t, ts...>();
    }
};

struct b {
    static const int i = -9;
};

struct c {
    static const int i = 4;
};


int main()
{
    a _a;

    _a.exec<b,c>();

    std::cout << _a.i << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

这个想法是从一组类中获取相同的信息,而不需要每个类的对象.

有谁知道这是否可能?

谢谢!

bar*_*top 5

如果您的编译器不支持C++ 17:

template <typename ...ts>
void f() {
    for ( const auto &j : { ts::i... } )
        i += j;
}
Run Code Online (Sandbox Code Playgroud)