您可以消除地址向量并遍历成员(不过,我保留了该向量)
#include <iostream>
#include <tuple>
#include <vector>
// Dedicated function
template <typename T, std::size_t I = 0, typename ...Tuple>
inline typename std::enable_if<I == sizeof...(Tuple), void>::type
collect_addresses(std::vector<T*>& result, std::tuple<Tuple...>&) {
}
template <typename T, std::size_t I = 0, typename ...Tuple>
inline typename std::enable_if<I < sizeof...(Tuple), void>::type
collect_addresses(std::vector<T*>& result, std::tuple<Tuple...>& tuple) {
result.push_back(&std::get<I>(tuple));
collect_addresses<T, I + 1, Tuple...>(result, tuple);
}
template <typename T, typename ...Tuple>
inline std::vector<T*> collect_addresses(std::tuple<Tuple...>& tuple) {
std::vector<T*> result;
result.reserve(sizeof...(Tuple));
collect_addresses(result, tuple);
return result;
}
// Static function [Tuple]
template <typename Function, std::size_t I = 0, typename ...Tuple>
inline typename std::enable_if<I == sizeof...(Tuple), void>::type
invoke_tuple(const Function&, std::tuple<Tuple...>&) {
}
template <typename Function, std::size_t I = 0, typename ...Tuple>
inline typename std::enable_if<I < sizeof...(Tuple), void>::type
invoke_tuple(const Function& function, std::tuple<Tuple...>& tuple) {
function(std::get<I>(tuple));
invoke_tuple<Function, I + 1, Tuple...>(function, tuple);
}
// Member function [Tuple]
template <typename Instance, typename Function, std::size_t I = 0, typename ...Tuple>
inline typename std::enable_if<I == sizeof...(Tuple), void>::type
invoke_tuple(Instance&, const Function&, std::tuple<Tuple...>&) {
}
template <typename Instance, typename Function, std::size_t I = 0, typename ...Tuple>
inline typename std::enable_if<I < sizeof...(Tuple), void>::type
invoke_tuple(Instance& instance, const Function& function, std::tuple<Tuple...>& tuple) {
(instance.*function)(std::get<I>(tuple));
invoke_tuple<Instance, Function, I + 1, Tuple...>(instance, function, tuple);
}
// Static function [Variadic Template]
template <typename Function>
inline void invoke(const Function&) {
}
template <typename Function, typename T, typename ...Args>
inline void invoke(const Function& function, T& value, Args&... args) {
function(value);
invoke(function, args...);
}
// Member function [Variadic Template]
template <typename Instance, typename Function>
inline void invoke(Instance&, const Function&) {
}
template <typename Instance, typename Function, typename T, typename ...Args>
inline void invoke(Instance& instance, const Function& function, T& value, Args&... args) {
(instance.*function)(value);
invoke(instance, function, args...);
}
class A {
// public in this test
public:
std::tuple<int, int, int> params;
std::vector<int*> addrs;
A() : addrs(collect_addresses<int>(params))
{}
};
class B {
private:
typedef std::tuple<int, int, int> Params;
// public in this test
public:
Params params;
std::vector<int*> addrs;
B()
{
addrs.reserve(std::tuple_size<Params>::value);
invoke_tuple([this](int& i) { addrs.push_back(&i); }, params);
}
};
class C {
// public in this test
public:
int a;
int b;
int c;
std::vector<int*> addrs;
C()
{
addrs.reserve(3);
invoke([this](int& i) { addrs.push_back(&i); }, a, b, c);
}
};
int main(){
A a;
for(int* p: a.addrs) std::cout << (const void*)p << std::endl;
B b;
for(int* p: b.addrs) std::cout << (const void*)p << std::endl;
C c;
for(int* p: c.addrs) std::cout << (const void*)p << std::endl;
}