在C ++程序中,我有两个类(结构),如
struct A
{
int x;
double y;
// other members
};
struct B
{
int x;
double y2;
// other members
};
我想以某种方式“绑定”相应的成员,例如A::x
到B::x
和A::y
到B::y2
。 “绑定”是指获取对绑定变量的引用的能力,例如,给定类A
的成员,我可以将其分配给相应的B
成员的值。
一旦我有这样的绑定,我想构建一个绑定表或类似的东西,我可以迭代。例如,这样可以将相应的字段从A a;
复制到B b;
,例如CopyBound(a, b, bind_table);
,但也可能执行其他一些不限于Copy
接口的事情。
这个bind_table的问题在于我想要静态类型,在这种情况下bind_table必须包含不同的类型。例如,一个指向类成员的指针表将包含&A::x
和&A::y
,但它们的类型不同,所以我不能把它们说成数组。
有什么想法可以方便地实现,尽可能多地进行编译时类型检查?
答案 0 :(得分:3)
也许你想要一些模板元编程:
#include <iostream>
// An item in the list of bound members
template<typename Class, typename T, T Class::*Ptr, typename Next = void> struct m
{
typedef Class containing_type;
typedef T value_type;
typedef Next next;
static T Class::*pointer;
};
template<typename Class, typename T, T Class::*Ptr, typename Next>
T Class::* m<Class, T, Ptr, Next>::pointer = Ptr;
// Iterator for the member list
template<typename Item1, typename Item2> struct apply_helper
{
typedef typename Item1::containing_type A;
typedef typename Item2::containing_type B;
template<typename Op>
static void apply(A& a, B& b, Op op)
{
op(a.*Item1::pointer, b.*Item2::pointer);
apply_helper<typename Item1::next, typename Item2::next>::apply(a, b, op);
}
};
template<typename Item1> struct apply_helper<Item1, void>
{
// An error referencing this means you forgot a member in the list
};
template<typename Item2> struct apply_helper<void, Item2>
{
// An error referencing this means you forgot a member in the list
};
template<> struct apply_helper<void, void>
{
template<typename A, typename B, typename Op>
static void apply(A& a, B& b, Op op) {}
};
// This function initiates the enumeration of the members
template<typename A, typename B, typename Op> void apply(A& a, B& b, Op op)
{
apply_helper<typename A::members, typename B::members>::apply(a, b, op);
}
// EXAMPLE
struct MyA
{
int v1;
double v2;
typedef m<MyA, int, &MyA::v1,
m<MyA, double, &MyA::v2> > members;
};
struct MyB
{
int v1;
double v2;
typedef m<MyB, int, &MyB::v1,
m<MyB, double, &MyB::v2> > members;
};
// A functor which demonstrates printing the value before copying
struct CopyAnyValue
{
template<typename T> void operator()(T& t1, T& t2)
{
std::cout << "t1 = " << t1 << std::endl;
t2 = t1;
}
};
int main()
{
MyA a;
MyB b;
a.v1 = 3;
a.v2 = 5.5;
apply(a, b, CopyAnyValue());
std::cout << "v1: " << b.v1 << " v2: " << b.v2 << std::endl;
return 0;
}