我一直在尝试实现一个需要部分模板专业化并回退到静态结构技术的函数,但我遇到了很多问题。
template<typename T> struct PushImpl<const T&> {
typedef T* result_type;
typedef const T& argument_type;
template<int StackSize> static result_type Push(IStack<StackSize>* sptr, argument_type ref) {
// Code if the template is T&
}
};
template<typename T> struct PushImpl<const T*> {
typedef T* result_type;
typedef const T* argument_type;
template<int StackSize> static result_type Push(IStack<StackSize>* sptr, argument_type ptr) {
return PushImpl<const T&>::Push(sptr, *ptr);
}
};
template<typename T> struct PushImpl {
typedef T* result_type;
typedef const T& argument_type;
template<int StackSize> static result_type Push(IStack<StackSize>* sptr, argument_type ref) {
// Code if the template is neither T* nor T&
}
};
template<typename T> typename PushImpl<T>::result_type Push(typename PushImpl<T>::argument_type ref) {
return PushImpl<T>::Push(this, ref);
}
首先:该结构嵌套在另一个类(提供 Push 作为成员函数的那个)中,但它无法访问模板参数(StackSize),即使我的其他嵌套类都可以。我已经解决了这个问题,但如果他们可以像普通类一样访问 StackSize 会更干净。
第二:编译器抱怨它不使用或不能推导出T。真的吗?
第三:编译器抱怨它不能在当前范围(类范围)中专门化一个模板。
我看不出有什么问题。我是否不小心调用了一些错误的语法?