我有一个概括SISO功能的课程:
scrollView.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {
@Override
public void onScrollChanged() {
// for normal ScrollView
int scrollY = rootScrollView.getScrollY();
// for HorizontalScrollView
int scrollX = rootScrollView.getScrollX();
}
});
然后我有一堆BinaryOperator类的子类:
template<typename T, typename S> struct Function {
virtual S operator()(const T& value) const = 0;
在这些之后,我终于可以实现用于将函数一起加,乘等的运算符。所以这些构建了一个AST,内存管理供以后使用。
template<typename T, typename S> struct BinaryOp : public Function<T, S> {
Function<T, S>& funcA;
Function<T, S>& funcB;
...
}
template<typename T, typename S> struct Addition : public BinaryOp<T, S> {
virtual inline S operator()(const T& value) const {
return funcA(value) + funcB(value);
}
};
我还实现了常用功能(正弦,余弦等):
inline Addition<T, S>& operator+(Function<T, S>& func) {
return *new Addition<T, S>(*this, func);
}
inline Addition<T, S>& operator+(const T& ref) {
return *new Addition<T, S>(*this, *new Const<T>(ref));
}
...
一旦我想实现嵌套函数[f(g(x))],就会出现问题。
template<typename T> struct Sine : public Function<T, T> {
virtual inline T operator()(const T& value) const {
return std::sin(value);
}
};
我尝试使用此操作符使用此草图但失败了。根据VS,它无法将余弦[T = float]转换为const float&amp;。所以它似乎试图使用operator()的第一个实现。
template<typename U> inline Inclusion<T, S, U> operator()(Function<U, S>& func) {
return *new Inclusion<T, S, U>(*this, func);
}
如果我不使用运算符()但是给它一些随机名称就行了。谁能告诉我为什么?或者更好的是如何在保持模板和运算符重载的同时完成这项工作?
产生错误的行:
Sine<float> sine = Sine<float>();
Cosine<float> cos = Cosine<float>();
Function<float, float>& func = sine(cos);
错误:
Function<float, float>& func = sine(cos);