aby*_*s.7 9 c++ boost argument-passing function-signature
是否有可能模仿这样的事情:
typedef boost::function<void(A)> B;
typedef boost::function<void(B)> A;
Run Code Online (Sandbox Code Playgroud)
主要目标是能够编写这样的代码(在伪c ++中):
void a_(B b) {
// ...
b(a_);
}
void b_(A a) {
// ...
f(boost::bind(a, b_));
}
f(boost::bind(a_, b_));
Run Code Online (Sandbox Code Playgroud)
不能直接使用 typedef 实现;无论何时使用 typedef,它都相当于原始类型,所以如果你写
typedef boost::function<void(A)> B;
typedef boost::function<void(B)> A;
Run Code Online (Sandbox Code Playgroud)
那么B将相当于boost::function<void(A)>,这相当于boost::function<void(boost::function<void(B)>)>,依此类推,直到你得到
boost::function<void(boost::function<void(boost::function<void(...)>)>)>
Run Code Online (Sandbox Code Playgroud)
,这是一种无限长度的类型。
但是,您可以(至少)将两种类型之一定义为 astruct或class:
struct A;
typedef boost::function<void(A)> B;
struct A
{
B b;
A(B b) : b(b) {}
// optional:
void operator() (A a) { b(a); }
};
Run Code Online (Sandbox Code Playgroud)
您可能需要添加更多构造函数和/或转换运算符以使类型的行为完全“透明”,或者您可以只显式访问该结构。